import os import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn import linear_model from sklearn import metrics from sklearn import tree from sklearn import neighbors from sklearn import svm from sklearn import ensemble from sklearn import cluster import seaborn as sns os.chdir(r'D:\projects\wordpress\ex48') os.getcwd() iris = datasets.load_iris() print(type(iris)) print(iris.target_names) print(iris.feature_names) print(iris.data.shape) print(iris.target.shape) X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, train_size=0.7) classifier = linear_model.LogisticRegression() print(classifier.fit(X_train, y_train)) y_test_pred = classifier.predict(X_test) print(metrics.classification_report(y_test, y_test_pred)) print(np.bincount(y_test)) print(metrics.confusion_matrix(y_test, y_test_pred)) classifier = tree.DecisionTreeClassifier() classifier.fit(X_train, y_train) y_test_pred = classifier.predict(X_test) print(metrics.confusion_matrix(y_test, y_test_pred)) classifier = neighbors.KNeighborsClassifier() classifier.fit(X_train, y_train) y_test_pred = classifier.predict(X_test) print(metrics.confusion_matrix(y_test, y_test_pred)) classifier = svm.SVC() classifier.fit(X_train, y_train) y_test_pred = classifier.predict(X_test) print(metrics.confusion_matrix(y_test, y_test_pred)) classifier = ensemble.RandomForestClassifier() classifier.fit(X_train, y_train) y_test_pred = classifier.predict(X_test) print(metrics.confusion_matrix(y_test, y_test_pred)) train_size_vec = np.linspace(0.1, 0.9, 30) classifiers = [tree.DecisionTreeClassifier, neighbors.KNeighborsClassifier, svm.SVC, ensemble.RandomForestClassifier ] cm_diags = np.zeros((3, len(train_size_vec), len(classifiers)), dtype=float) for n, train_size in enumerate(train_size_vec): X_train, X_test, y_train, y_test = \ train_test_split(iris.data, iris.target, train_size=train_size) [crayon-67674d732bb9e019976293/] fig, axes = plt.subplots(4, 1, figsize=(6,18)) for m, Classifier in enumerate(classifiers): axes[m].plot(train_size_vec, cm_diags[2, :, m], label=iris.target_names[2]) axes[m].plot(train_size_vec, cm_diags[1, :, m], label=iris.target_names[1]) axes[m].plot(train_size_vec, cm_diags[0, :, m], label=iris.target_names[0]) axes[m].set_title(type(Classifier()).<strong>name</strong>) axes[m].set_ylim(0, 1.1) axes[m].set_xlim(0.1, 0.9) axes[m].set_ylabel("classification accuracy") axes[m].set_xlabel("training size ratio") axes[m].legend(loc=4) fig.tight_layout() plt.savefig("example48.png", dpi=100) plt.show() plt.close()
Recent Comments