鸢尾花数据集:自定义感知机与sklearn感知机效果大对比!附代码与图像展示
以下是为您提供的一段可能的代码示例,希望对您有所帮助: ```python import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.linear_model import Perceptron # 加载鸢尾花数据集 iris = datasets.load_iris() X = iris.data[:, :2] # 只取前两个特征 y = iris.target # 自定义感知机函数 class CustomPerceptron: def __init__(self, learning_rate=0.1, n_iterations=100): self.learning_rate = learning_rate self.n_iterations = n_iterations self.weights = None self.bias = None def fit(self, X, y): n_samples, n_features = X.shape self.weights = np.zeros(n_features) self.bias = 0 for _ in range(self.n_iterations): for i in range(n_samples): linear_output = np.dot(X[i], self.weights) + self.bias prediction = np.sign(linear_output) if prediction!= y[i]: self.weights += self.learning_rate * y[i] * X[i] self.bias += self.learning_rate * y[i] def predict(self, X): linear_output = np.dot(X, self.weights) + self.bias return np.sign(linear_output) # 创建自定义感知机对象并训练 custom_perceptron = CustomPerceptron() custom_perceptron.fit(X, y) # 使用 sklearn 的感知机 sklearn_perceptron = Perceptron() sklearn_perceptron.fit(X, y) # 绘制图像 plt.figure(figsize=(10, 5)) plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='red', label='Class 0') plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='blue', label='Class 1') # 绘制自定义感知机的决策边界 x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, 0.01), np.arange(x2_min, x2_max, 0.01)) Z = custom_perceptron.predict(np.c_[xx1.ravel(), xx2.ravel()]) Z = Z.reshape(xx1.shape) plt.contour(xx1, xx2, Z, levels=[0], colors='green', linestyles='dashed') # 绘制 sklearn 感知机的决策边界 Z_sklearn = sklearn_perceptron.predict(np.c_[xx1.ravel(), xx2.ravel()]) Z_sklearn = Z_sklearn.reshape(xx1.shape) plt.contour(xx1, xx2, Z_sklearn, levels=[0], colors='black', linestyles='dashed') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.title('Comparison of Custom and sklearn Perceptron on Iris Dataset') plt.legend() plt.show() ``` 额外需要注意的逻辑点: 1. 数据的预处理和特征选择可能会影响模型效果。 2. 调整学习率和迭代次数以优化模型性能。 3. 对于复杂的数据集,感知机可能表现不佳,考虑使用更复杂的模型。 ######[AI写代码神器 | 821点数解答 | 2024-12-10 22:30:51]
- 用 NumPy 与 Scikit-learn 实现线性回归:模拟数据、训练模型、评估与可视化全流程(阿里通义 | 651点数解答 | 2025-06-12 13:18:59)133
- Python代码实现:用线性回归模型根据年龄、体重和血压预测胆固醇水平(阿里通义 | 404点数解答 | 2025-06-29 17:53:11)129
- Java代码实现从JSON数据中提取ID,涉及“数据”与“项目 - 知识库”结构(字节豆包 | 603点数解答 | 2025-04-15 22:48:51)255
- C++ 实现:计算一排鹅与最高、最矮鹅的身高差及代码详解(字节豆包 | 549点数解答 | 2025-11-16 20:19:06)105
- 编程揭秘:计算 𝑛! 在 𝑃 进制下末尾零个数的方法与代码实现(DeepSeek | 549点数解答 | 2026-01-11 17:49:54)63
- Android开发自定义 Handler :七大策略有效规避内存泄漏!(百度文心 | 526点数解答 | 2023-11-09 17:34:21)306
- 产品缺陷频数监控:加权卡方与休哈特控制图实战应用(阿里通义 | 925点数解答 | 2025-04-05 17:47:55)233
- Python 实现未完成任务重新定价效果评估:成本增加额与新增任务完成量计算(字节豆包 | 612点数解答 | 2025-05-04 22:31:18)148
- Python 实现:未完成任务重新定价效果评估(成本增加额与新增任务完成量计算)(阿里通义 | 578点数解答 | 2025-05-04 22:36:52)178
- Keil uVision5:4 按键(K1-K4)精准控制声光模式切换,附详尽源码!(字节豆包 | 1342点数解答 | 2024-10-24 16:46:48)299
- Keil uVision5:4按键(K1 - K4)控制声光模式切换源码大公开!(阿里通义 | 1570点数解答 | 2024-10-24 16:49:54)304
- 揭秘!无名杀“重炮”技能代码全解析与注意要点(字节豆包 | 381点数解答 | 2025-03-21 22:34:23)172