model
1 2 3
| from sklearn.ensemble import RandomForestClassifier
rf_clf_model = RandomForestClassifier(n_estimators=150, max_depth=7, random_state=42)
|
metric
accuracy,precision,rcall,
f1_score
1
| from sklearn.metrics import accuracy_score, f1_score,precision_score,recall_score
|
model_selection
交叉验证 cross_val_score
1
| from sklearn.model_selection import cross_val_score
|
cross_val_score交叉验证
首个参数为estimator(也就是要执行fit的model),cv
折数,默认是5折验证,返回的cv长度的score
参考 https://blog.csdn.net/qq_36523839/article/details/80707678
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from sklearn import datasets from sklearn.model_selection import train_test_split,cross_val_score from sklearn.neighbors import KNeighborsClassifier import matplotlib.pyplot as plt iris = datasets.load_iris() X = iris.data y = iris.target train_X,test_X,train_y,test_y = train_test_split(X,y,test_size=1/3,random_state=3) k_range = range(1,31) cv_scores = [] for n in k_range: knn = KNeighborsClassifier(n) scores = cross_val_score(knn,train_X,train_y,cv=10,scoring='accuracy') cv_scores.append(scores.mean()) plt.plot(k_range,cv_scores) plt.xlabel('K') plt.ylabel('Accuracy') plt.show() best_knn = KNeighborsClassifier(n_neighbors=3) best_knn.fit(train_X,train_y) print(best_knn.score(test_X,test_y))
|