Introduction: classification is an important function of a machine learning, machine learning in the study of history, the birth of a large number of classification algorithms, and each algorithm has its advantages and disadvantages.
This article summarizes the commonly used classification algorithm and its implementation for quick queries. (As used herein, iris data set is a three-category classification)
This article nine kinds of species classification algorithm accuracy on the test set (accuracy) are as follows:
[1.0, 1.0, 1.0, 1.0, 0.9666666666666667, 1.0, 0.9666666666666667, 1.0, 0.9666666666666667]
1 import numpy as np 2 import pandas as pd 3 import matplotlib as mpl 4 import matplotlib.pyplot as plt 5 import sklearn 6 from sklearn import datasets 7 from sklearn.metrics import accuracy_score 8 9 from sklearn.ensemble import RandomForestClassifier 10 from sklearn.tree import DecisionTreeClassifier 11 12 from sklearn.neighbors import KNeighborsClassifier 13 from sklearn.svm import SVC, LinearSVC 14 from sklearn.linear_model import LogisticRegression 15 16 from sklearn.linear_model import SGDClassifier 17 from sklearn.linear_model import Perceptron 18 from sklearn.naive_bayes import GaussianNB 19 20 from sklearn.model_selection import train_test_split 21 from sklearn.model_selection import cross_val_score 22 23 from sklearn.model_selection import GridSearchCV 24 25 iris = datasets.load_iris() 26X, Y = iris.data, iris.target 27 28 x_train, x_test, y_train, android.permission.FACTOR. train_test_split = (X, Y, test_size = 0.2 , random_state = 0 ) 29 30 RES = [] 31 is 32 # . 1 . random forest classifier 33 Print ( ' random forest classifier ' ) 34 is CLF = RandomForestClassifier (n_estimators = 100 ) 35 clf.fit (x_train, y_train) 36 cross_score = cross_val_score (CLF, x_train, y_train, CV = . 3 , Scoring = " Accuracy " ) 37 [ print(cross_score) 38 y_predict = clf.predict(x_test) 39 score = accuracy_score(y_test,y_predict) 40 res.append(score) 41 print() 42 43 #2. 决策树分类 44 print('决策树分类') 45 clf = DecisionTreeClassifier() 46 clf.fit(x_train, y_train) 47 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 48 print(cross_score) 49 y_predict = clf.predict(x_test) 50 score = accuracy_score(y_test,y_predict) 51 res.append(score) 52 print() 53 54 #3. KNN 55 print('KNN') 56 clf = KNeighborsClassifier(n_neighbors = 13) 57 clf.fit(x_train, y_train) 58 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 59 print(cross_score) 60 y_predict = clf.predict(x_test) 61 score = accuracy_score(y_test,y_predict) 62 res.append(score) 63 print() 64 65 #4. SVM分类 66 print('SVM') 67 clf = SVC(gamma='scale') 68 clf.fit(x_train, y_train) 69 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 70 print(cross_score) 71 y_predict = clf.predict(x_test) 72 score = accuracy_score(y_test,y_predict) 73 res.append(score) 74 print() 75 76 #5. 逻辑回归分类 77 print('LogisticRegression') 78 clf = LogisticRegression(solver='lbfgs',multi_class='ovr') 79 clf.fit(x_train, y_train) 80 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 81 print(cross_score) 82 y_predict = clf.predict(x_test) 83 score = accuracy_score(y_test,y_predict) 84 res.append(score) 85 print() 86 87 #6. linear svm分类 88 print('linear SVM') 89 clf = LinearSVC(max_iter=10000) 90 clf.fit(x_train, y_train) 91 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 92 print(cross_score) 93 y_predict = clf.predict(x_test) 94 score = accuracy_score(y_test,y_predict) 95 res.append(score) 96 print() 97 98 #7. 随机梯度下降分类 99 print('SGD') 100 clf = SGDClassifier(max_iter=1000,tol=1e-3) 101 clf.fit(x_train, y_train) 102 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 103 print(cross_score) 104 y_predict = clf.predict(x_test) 105 score = accuracy_score(y_test,y_predict) 106 res.append(score) 107 print() 108 109 #8. 感知机分类 110 print('Perceptron') 111 clf = Perceptron(max_iter=1000,tol=1e-3) 112 clf.fit(x_train, y_train) 113 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 114 print(cross_score) 115 y_predict = clf.predict(x_test) 116 score = accuracy_score(y_test,y_predict) 117 res.append(score) 118 print() 119 120 #9. 朴素贝叶斯分类 121 print('Naive Bayes') 122 clf = GaussianNB() 123 clf.fit(x_train, y_train) 124 cross_score = cross_val_score(clf, x_train, y_train, cv=3, scoring="accuracy") 125 print(cross_score) 126 y_predict = clf.predict(x_test) 127 score = accuracy_score(y_test,y_predict) 128 res.append (Score) 129 Print () 130. 131 is # 10 . Comparison of the scores 132 Print (RES)