感知机具体说明:见《统计学习方法第二章》。
实现(scikit-learn):
数据集
1 import numpy as np 2 import matplotlib.pyplot as plt 3 from sklearn.linear_model import perceptron 4 5 # Data 6 d = np.array([ 7 [2, 1, 2, 5, 7, 2, 3, 6, 1, 2, 5, 4, 6, 5], 8 [2, 3, 3, 3, 3, 4, 4 ,4 , 5, 5, 5, 6, 6, 7] 9 ]) 10 11 # Labels 12 t = np.array([0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1]) 13 14 # plot the data 15 colormap = np.array(['r', 'k']) 16 plt.scatter(d[0], d[1], c = colormap[t], s = 40) 17 plt.show()
数据集表示:
处理数据用坐标表示:
1 # rotate the data 180 degrees 2 d90 = np.rot90(d) 3 d90 = np.rot90(d90) 4 d90 = np.rot90(d90)
1 # # create the model 2 net = perceptron.Perceptron(n_iter = 100, verbose = 0, random_state = None, fit_intercept = True, eta0 = 0.002) 3 net.fit(d90, t) 4 5 # # print the results 6 print "Prediction" + str(net.predict(d90)) 7 print "Actual " + str(t) 8 print "Accuracy " + str(net.score(d90, t) * 100) + "%"
测试模型:
1 nX = np.random.random_integers(10, size=(2, 50)) 2 3 # Rotate it the same as the previous data 4 nX90 = np.rot90(nX) 5 nX90 = np.rot90(nX90) 6 nX90 = np.rot90(nX90) 7 8 # # set predication as the predication results 9 predication = net.predict(nX90) 10 11 # # Print to have a look 12 print predication 13 plt.scatter(nX[0], nX[1], c = colormap[predication], s = 40) 14 15 # now plot the hyperplane 16 ymin, ymax = plt.ylim() 17 # # cal 18 w = net.coef_[0] 19 a = -w[0] / w[1] 20 xx = np.linspace(ymin, ymax) 21 yy = a * xx - (net.intercept_[0])/ w[1] 22 plt.plot(yy, xx, 'k-') 23 plt.show()
C++描述:
1 /** 2 * 求两个向量的内积 3 * 4 * @param lhs 向量1 5 * @param rhs 向量2 6 * 7 * @return 内积 8 */ 9 int product(const vector<int> &lhs, const vector<int> &rhs) { 10 int result = 0; 11 for (int i = 0; i < lhs.size() - 1; ++i) { 12 result += lhs[i] * rhs[i]; 13 } 14 15 return result; 16 } 17 18 /** 19 * 感知机, 原始形式 20 * 21 * @param dataSet 数据集 22 * 23 * @return 返回结果(w, b) 24 */ 25 vector<int> perceptron(const vector<vector<int>> &dataSet) { 26 if (dataSet.size() == 0) { 27 return vector<int>(); 28 } 29 30 auto len = dataSet[0].size(); 31 vector<int> w_b(len, 0); 32 int n = 1; 33 while (true) { 34 bool finished = true; 35 for (auto &ix : dataSet) { 36 int y = ix[len - 1]; 37 if (y * (product(ix, w_b) + w_b[len - 1]) <= 0) { 38 for (auto j = 0; j < len - 1; ++j) { 39 w_b[j] += n * y * ix[j]; 40 } 41 w_b[len - 1] += n * y; 42 finished = false; 43 } 44 } 45 46 if (finished) { 47 break; 48 } 49 } 50 51 return w_b; 52 }
1 /** 2 * 感知机,对偶形式 3 * 4 * @param dataSet 数据集 5 * 6 * @return 结果 7 */ 8 vector<int> perceptron_s(const vector<vector<int>> &dataSet) { 9 // 计算Gram矩阵 10 vector<vector<int>> gram; 11 for (auto &i : dataSet) { 12 vector<int> tmp; 13 for (auto &j : dataSet) { 14 tmp.push_back(product(i, j)); 15 } 16 gram.push_back(tmp); 17 } 18 19 vector<int> a(dataSet.size(), 0); 20 int b = 0; 21 int n = 0.8; 22 23 auto len = dataSet[0].size(); 24 while (true) { 25 bool finished = true; 26 for (int i = 0; i < dataSet.size(); ++i) { 27 int sum = 0; 28 for (int j = 0; j < dataSet.size(); ++j) { 29 int yj = dataSet[j][len - 1]; 30 sum += a[i] * gram[i][j] * yj; 31 } 32 33 int yi = dataSet[i][len - 1]; 34 if (yi *(sum + b) <= 0) { 35 a[i] += n; 36 b += n * yi; 37 finished = false; 38 cout << a[0] << " " << a[1] << " " << a[2] << " " << b << endl; 39 } 40 } 41 42 if (finished) { 43 break; 44 } 45 } 46 47 vector<int> w_b; 48 for (int i = 0; i < dataSet.size(); ++i) { 49 int tmp = 0; 50 for (int j = 0; j < dataSet.size(); ++j) { 51 tmp += dataSet[i][j] * a[j]; 52 } 53 w_b.push_back(tmp); 54 } 55 56 w_b.push_back(b); 57 return w_b; 58 }