Training Perception with scikit-learn
1 from sklearn import datasets 2 import numpy as np 3 4 iris = datasets.load_iris() 5 6 X = iris.data[:, [2, 3]] 7 y = iris.target 8 print('Class labels:', np.unique(y)) 9 10 from sklearn.model_selection import train_test_split 11 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 1, stratify = y) 12 13 print('Labels counts in y:', np.bincount(y)) 14 print('Labels counts in y_train:', np.bincount(y_train)) 15 print('Labels counts in y_test:', np.bincount(y_test)) 16 17 from sklearn.preprocessing import StandardScaler 18 sc = StandardScaler() 19 sc.fit(X_train) 20 X_train_std = sc.transform(X_train) 21 X_test_std = sc.transform(X_test) 22 23 from sklearn.linear_model import Perceptron 24 25 ppn = Perceptron(max_iter=40, eta0=0.1, random_state = 1) 26 ppn.fit(X_train_std, y_train) 27 28 y_pred = ppn.predict(X_test_std) 29 print('Misclassified samples: %d' % (y_test != y_pred).sum()) 30 31 from sklearn.metrics import accuracy_score 32 print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) 33 print('Accuracy: %.2f' % ppn.score(X_test_std, y_test)) 34 35 36 import matplotlib.pyplot as plt 37 from matplotlib.colors import ListedColormap 38 39 def plot_decision_regions(X, y, classifier, test_idx = None, resolution = 0.02): 40 #setup marker generator and color map 41 markers = ('s', 'x', 'o', '^', 'v') 42 colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan') 43 cmap = ListedColormap(colors[:len(np.unique(y))]) 44 45 #plot the decision surface 46 x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 47 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 48 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), 49 np.arange(x2_min, x2_max, resolution)) 50 Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) 51 Z = Z.reshape(xx1.shape) 52 plt.contourf(xx1, xx2, Z, alpha=0.3, cmap = cmap) 53 plt.xlim(xx1.min(), xx2.max()) 54 plt.ylim(xx2.min(), xx2.max()) 55 56 for idx, cl in enumerate(np.unique(y)): 57 plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], 58 alpha=0.8, c=colors[idx], 59 marker = markers[idx], label=cl, 60 edgecolor='black') 61 62 #highlight test samples 63 if test_idx: 64 #plot all samples 65 X_test, y_test = X[test_idx, :], y[test_idx] 66 67 plt.scatter(X_test[:, 0], X_test[:, 1], c='', 68 edgecolor = 'black', alpha=1.0, 69 linewidth=1, marker='o', s=100, 70 label='test set') 71 72 X_combined_std = np.vstack((X_train_std, X_test_std)) 73 y_combined = np.hstack((y_train, y_test)) 74 plot_decision_regions(X=X_combined_std, 75 y=y_combined, 76 classifier = ppn, 77 test_idx = range(105, 150) 78 ) 79 plt.xlabel('petal length [standardized]') 80 plt.ylabel('petal width [standardized]') 81 plt.legend(loc='upper left') 82 plt.show()