DL之Perceptron&AdalineGD:基于iris莺尾花数据集利用Perceptron感知机和AdalineGD算法实现二分类

DL之Perceptron&AdalineGD:基于iris莺尾花数据集利用Perceptron感知机和AdalineGD算法实现二分类


相关文章
DL之Perceptron&AdalineGD:基于iris莺尾花数据集利用Perceptron感知机和AdalineGD算法实现二分类
DL之Perceptron&AdalineGD:基于iris莺尾花数据集利用Perceptron感知机和AdalineGD算法实现二分类实现

基于iris莺尾花数据集利用Perceptron感知机算法实现二分类

设计思路

输出结果

<bound method DataFrame.info of      SepalLength_cm  SepalWidth_cm  ...  PetalWidth_cm           label
0               5.1            3.5  ...            0.2     Iris-setosa
1               4.9            3.0  ...            0.2     Iris-setosa
2               4.7            3.2  ...            0.2     Iris-setosa
3               4.6            3.1  ...            0.2     Iris-setosa
4               5.0            3.6  ...            0.2     Iris-setosa
..              ...            ...  ...            ...             ...
145             6.7            3.0  ...            2.3  Iris-virginica
146             6.3            2.5  ...            1.9  Iris-virginica
147             6.5            3.0  ...            2.0  Iris-virginica
148             6.2            3.4  ...            2.3  Iris-virginica
149             5.9            3.0  ...            1.8  Iris-virginica

[150 rows x 5 columns]>
   SepalLength_cm  SepalWidth_cm  ...  PetalWidth_cm            label
0             5.0            3.5  ...            0.3      Iris-setosa
1             6.3            2.5  ...            1.9   Iris-virginica
2             4.4            3.0  ...            0.2      Iris-setosa
3             5.7            2.8  ...            1.3  Iris-versicolor
4             6.8            3.2  ...            2.3   Iris-virginica

[5 rows x 5 columns]
Iris-setosa        50
Iris-versicolor    50
Iris-virginica     50
Name: label, dtype: int64
(150, 2)
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0]
CE_res (37, 0, 0, 0)
recall  precision  accurency
1.0 1.0 1.0

核心代码

class Perceptron(object):
   def __init__(self, rate = 0.01, niter = 10):
      self.rate = rate
      self.niter = niter

   def fit(self, X, y):
      self.weight = np.zeros(1 + X.shape[1])

      self.errors = []  # Number of misclassifications

      for i in range(self.niter):
         err = 0
         for xi, target in zip(X, y):
            delta_w= self.rate * (target-self.predict(xi))
            self.weight[1:] += delta_w * xi
            self.weight[0] += delta_w
            err += int(delta_w != 0.0)
         self.errors.append(err)
      return self

   def net_input(self, X):
      return np.dot(X, self.weight[1:]) + self.weight[0]

   def predict(self, X):
      return np.where(self.net_input(X) >= 0.0, 1, -1)
(0)

相关推荐