X_test_std = sc.transform(X_test) # train the LogisticRegression svm = SVC(kernel='linear', C=1.0, random_state=0) svm.fit(X_train_std, y_train) # make predictions y_pred = svm.predict(X_test_std) print('Misclassified samples: %d' % (y_test != y_pred).sum()) print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) # graph decision regions X_combined_std = np.vstack((X_train_std, X_test_std)) y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X=X_combined_std, y=y_combined, classifier=svm, test_idx=range(105, 150)) plt.xlabel('petal length [standardized]') plt.ylabel('petal width [standardized]') plt.legend(loc='upper left') plt.tight_layout() # plt.savefig('./figures/support_vector_machine_scikit.png', dpi=300) plt.show() svm = SVC(kernel='rbf', random_state=0, gamma=0.2, C=1.0) svm.fit(X_train_std, y_train) plot_decision_regions(X_combined_std, y_combined, classifier=svm, test_idx=range(105, 150)) plt.xlabel('petal length [standardized]') plt.ylabel('petal width [standardized]')
'https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data', header=None) X, y = df_wine.iloc[:, 1:].values, df_wine.iloc[:, 0].values # train用とtest用に分割 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=0) # データ標準化 sc = StandardScaler() X_train_std = sc.fit_transform(X_train) X_test_std = sc.transform(X_test) pca = PCA(n_components=2) lr = LogisticRegression() X_train_pca = pca.fit_transform(X_train_std) X_test_pca = pca.transform(X_test_std) lr.fit(X_train_pca, y_train) plot_decision_regions(X_train_pca, y_train, classifier=lr) plt.legend(loc='lower left') plt.tight_layout() plt.show()
y, test_size=0.3, random_state=0) # train the LogisticRegression forest = RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1, n_jobs=2) forest.fit(X_train, y_train) # make predictions y_pred = forest.predict(X_test) print('Misclassified samples: %d' % (y_test != y_pred).sum()) print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) # graph decision regions X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X_combined, y_combined, classifier=forest, test_idx=range(105, 150)) plt.xlabel('petal length [cm]') plt.ylabel('petal width [cm]') plt.legend(loc='upper left') plt.tight_layout() # plt.savefig('./figures/random_forest.png', dpi=300) plt.show()
X_xor = np.random.randn(200, 2) y_xor = np.logical_xor(X_xor[:, 0] > 0, X_xor[:, 1] > 0) y_xor = np.where(y_xor, 1, -1) plt.scatter(X_xor[y_xor == 1, 0], X_xor[y_xor == 1, 1], c='b', marker='x', label='1') plt.scatter(X_xor[y_xor == -1, 0], X_xor[y_xor == -1, 1], c='r', marker='s', label='-1') plt.xlim([-3, 3]) plt.ylim([-3, 3]) plt.legend(loc='best') plt.tight_layout() # plt.savefig('./figures/xor.png', dpi=300) plt.show() svm = SVC(kernel='rbf', random_state=0, gamma=0.10, C=10.0) svm.fit(X_xor, y_xor) plot_decision_regions(X_xor, y_xor, classifier=svm) plt.legend(loc='upper left') plt.tight_layout() # plt.savefig('./figures/support_vector_machine_rbf_xor.png', dpi=300) plt.show()
stratify=y) # データの標準化 sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) # パーセプトロン ppn = Perceptron(max_iter=40, eta0=0.1, random_state=1) ppn.fit(X_train_std, y_train) y_pred = ppn.predict(X_test_std) # ロジスティック回帰 lr = LogisticRegression(C=100.0, random_state=1) lr.fit(X_train_std, y_train) #print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) #print('Accuracy: %.2f' % ppn.score(X_test_std, y_test)) # trainとtestを結合する X_combined_std = np.vstack((X_train_std, X_test_std)) y_combined = np.hstack((y_train, y_test)) graph.plot_decision_regions(X=X_combined_std, y=y_combined, classifier=lr, test_idx=range(105, 150)) plt.legend(loc='upper left') plt.tight_layout() plt.show()
return np.where(self.net_input(X) >= 0.0, 1, -1) if __name__ == '__main__': import pandas as pd import matplotlib.pyplot as plt import graph # irisデータセット読み込み df = pd.read_csv('https://archive.ics.uci.edu/ml/' 'machine-learning-databases/iris/iris.data', header=None) y = df.iloc[:100, 4].values y = np.where(y == 'Iris-setosa', -1, 1) X = df.iloc[:100, [0, 2]].values ppn = Perceptron(eta=0.1, n_iter=10) ppn.fit(X, y) # グラフ描画 graph.plot_decision_regions(X, y, classifier=ppn) #plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o') #plt.xlabel('Epochs') #plt.ylabel('Number of update') plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend(loc='upper left') plt.show()