随即森林
from sklearn import neighbors, datasets, preprocessing from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from sklearn.tree import DecisionTreeClassifier iris = datasets.load_iris() X,y = iris.data[:,:2],iris.target X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33) scaler = preprocessing.StandardScaler().fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) knn = neighbors.KNeighborsClassifier(n_neighbors=5) knn.fit(X_train,y_train) y_pred = knn.predict(X_test) sum(y_test == y_pred)/y_test.shape[0] accuracy_score(y_test,y_pred)
决策树:
from sklearn import neighbors, datasets, preprocessing from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from sklearn.tree import DecisionTreeClassifier from sklearn.tree import DecisionTreeClassifier iris = datasets.load_iris() X,y = iris.data[:,:2],iris.target X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33) scaler = preprocessing.StandardScaler().fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) dt = DecisionTreeClassifier() dt.fit(X_train,y_train) tree_result = dt.predict(X_test) accuracy_score(tree_result,y_test)