x = iris.data[:, [2, 3]] y = iris.target x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1, stratify=y) #%% sc = StandardScaler() sc.fit(x_train) x_train_std = sc.transform(x_train) x_test_std = sc.transform(x_test) x_combined_std = np.vstack((x_train_std, x_test_std)) y_combined = np.hstack((y_train, y_test)) #%% knn = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski') knn.fit(x_train_std, y_train) #%% drp.plot_decision_regions(x=x_combined_std, y=y_combined, classifier=knn, test_idx=range(105, 150)) plt.xlabel('petal length [standardized]') plt.ylabel('petal width [standardized]') plt.legend(loc='upper left') plt.tight_layout() plt.show()
from layers import decisionregionplotfunction as drp from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.tree import DecisionTreeClassifier from sklearn.tree import export_graphviz from pydotplus import graph_from_dot_data #%% iris=datasets.load_iris() x=iris.data[:,[2,3]] y=iris.target x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,stratify=y) x_combined=np.vstack((x_train,x_test)) y_combined=np.hstack((y_train,y_test)) #%% tree=DecisionTreeClassifier(criterion='gini',max_depth=4,random_state=1) tree.fit(x_train,y_train) #%% drp.plot_decision_regions(x_combined,y_combined,classifier=tree,test_idx=range(105,150)) plt.xlabel('petal length [cm]') plt.ylabel('petal width [cm]') plt.legend(loc='upper left') plt.tight_layout() plt.show() #%% dot_data=export_graphviz(tree,filled=True,rounded=True,class_names=['Setosa','Versicolor','Virginica'],feature_names=['petal length','petal width'],out_file=None) graph=graph_from_dot_data(dot_data) graph.write_png('tree.png')
import numpy as np ''' import os import sys sys.path.append(os.path.join(os.path.dirname(__file__),'..')) ''' import layers from layers import classes as ccc from layers import decisionregionplotfunction as drp df = pd.read_csv( 'http://archive.ics.uci.edu/ml/' 'machine-learning-databases/iris/iris.data', header=None) df.tail() y = df.iloc[0:100, 4].values y = np.where(y == 'Iris-setosa', 0, 1) x = df.iloc[0:100, [0, 2]].values lrgd = ccc.LogisticRegressionGD(eta=0.05, n_iter=1000, random_state=1) lrgd.fit(x, y) drp.plot_decision_regions(x=x, y=y, classifier=lrgd) plt.xlabel('sepal length [standardaized]') plt.ylabel('petal length [standardized]') plt.legend(loc='upper left') plt.tight_layout() plt.show()
plt.show() """ #%% #↓は正規化済み+決定領域を示すグラフが出てくる(決定領域のグラフの仕組みはまだわかってない) x_std = np.copy(x) x_std[:, 0] = (x[:, 0] - x[:, 0].mean()) / x[:, 0].std() x_std[:, 1] = (x[:, 1] - x[:, 1].mean()) / x[:, 1].std() #%% #ada=ccc.AdalineGD(n_iter=15,eta=0.01) #ada.fit(x_std,y) ads = ccc.AdalineGD_stochasticgradientdescent(n_iter=15, eta=0.01) ads.fit(x_std, y) #%% #drp.plot_decision_regions(x_std,y,classifier=ada) drp.plot_decision_regions(x_std, y, classifier=ads) plt.xlabel('sepal length[standardized]') plt.ylabel('petal length[standardized]') plt.legend(loc='upper left') plt.tight_layout() plt.show() #%% plt.plot(range(1, len(ads.cost_) + 1), ads.cost_, marker='o') plt.xlabel('Epochs') plt.ylabel('sumsquarederror') plt.tight_layout()