感知机 Perceptron

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import torch
  4. %matplotlib inline
  5. %config InlineBackend.figure_format = 'svg'

加载数据集

数据集如下所示:

  1. -0.63 -1.53 0
  2. -1.08 -1.23 0
  3. 0.39 -1.99 0
  4. -1.69 0.80 0
  5. -1.52 -1.14 0
  6. 3.88 0.65 1
  7. 0.73 2.97 1
  8. 0.83 3.94 1
  9. 1.59 1.25 1
  10. 3.92 3.48 1

第一二列为 x,y 坐标,最后一列为正负标签,正负样本各有50个。

数据预处理

将数据打散,并取70%作为训练集,30%作为测试集。

并将数据进行归一化处理。

  1. data = np.genfromtxt('../data/perceptron_toydata.txt', delimiter='\t')
  2. # data.shape = (100, 3)
  3. X, y = data[:, :2], data[:, 2]
  4. # X.shape = (100, 2)
  5. y = y.astype(np.int)
  6. print('Class label counts:', np.bincount(y))
  7. # Class label counts: [50 50]
  8. # Shuffling & train/test split
  9. shuffle_idx = np.arange(y.shape[0])
  10. shuffle_rng = np.random.RandomState(123)
  11. shuffle_rng.shuffle(shuffle_idx)
  12. X, y = X[shuffle_idx], y[shuffle_idx]
  13. X_train, X_test = X[shuffle_idx[:70]], X[shuffle_idx[70:]]
  14. y_train, y_test = y[shuffle_idx[:70]], y[shuffle_idx[70:]]
  15. # Normalize (mean zero, unit variance)
  16. mu, sigma = X_train.mean(axis=0), X_train.std(axis=0)
  17. X_train = (X_train - mu) / sigma
  18. X_test = (X_test - mu) / sigma

接下来是把训练集的数据点可视化:

注意:直接用 plt.savefig() 会导出空白图片

  1. plt.scatter(X_train[y_train==0, 0], X_train[y_train==0, 1], label='class 0', marker='o')
  2. plt.scatter(X_train[y_train==1, 0], X_train[y_train==1, 1], label='class 1', marker='s')
  3. plt.xlabel('feature 1')
  4. plt.ylabel('feature 2')
  5. plt.legend()
  6. fig=plt.gcf()
  7. plt.show()
  8. fig.savefig("01.svg")

感知机 Perceptron - 图1

感知机模型

image.png

感知机 Perceptron - 图3

我们根据感知机的算法,能很轻松地写出模型代码:

  1. epoch=5
  2. N=epoch*X.shape[0]
  3. w=torch.randn(2)
  4. b=torch.randn(1)
  5. inx=torch.randint(low=0, high=X.shape[0],size=(N,))
  6. for k in range(N):
  7. i=inx[k]
  8. if y[i]*(w@X[i]+b) <0:
  9. w=w+y[i]*X[i]
  10. b=b+y[i]

迭代5个epoch后,在训练集和测试集上的结果如下:

感知机 Perceptron - 图4

可视化

  1. x_min = -2
  2. y_min = ( (-(w[0] * x_min) - b[0]) / w[1] )
  3. x_max = 2
  4. y_max = ( (-(w[0] * x_max) - b[0]) / w[1] )
  5. fig, ax = plt.subplots(1, 2, sharex=True, figsize=(7, 3))
  6. ax[0].plot([x_min, x_max], [y_min, y_max])
  7. ax[1].plot([x_min, x_max], [y_min, y_max])
  8. ax[0].scatter(X_train[y_train==0, 0], X_train[y_train==0, 1], label='class 0', marker='o')
  9. ax[0].scatter(X_train[y_train==1, 0], X_train[y_train==1, 1], label='class 1', marker='s')
  10. ax[1].scatter(X_test[y_test==0, 0], X_test[y_test==0, 1], label='class 0', marker='o')
  11. ax[1].scatter(X_test[y_test==1, 0], X_test[y_test==1, 1], label='class 1', marker='s')
  12. ax[1].legend(loc='upper left')
  13. fig=plt.gcf()
  14. plt.show()
  15. fig.savefig("02.svg")

参考资料

https://machinelearningmastery.com/implement-perceptron-algorithm-scratch-python/