随机森林和KNN分类结果可视化(Sklearn)

代码如下:

from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import  matplotlib.pyplot as plt





if __name__ == '__main__':
   wine=load_wine()
   Xtrain,Xtest,Ytrain,Ytest=train_test_split(wine.data,wine.target,test_size=0.3)
   rfc=RandomForestClassifier(n_estimators=25)
   rfc_s=cross_val_score(rfc,wine.data,wine.target,cv=10)
   knn=KNeighborsClassifier(n_neighbors=30)
   knn_s=cross_val_score(knn,wine.data,wine.target,cv=10)
   knn.fit(Xtrain,Ytrain)
   rfc.fit(Xtrain,Ytrain)
   plt.plot(range(1,11),rfc_s,label="RandomForest")
   plt.plot(range(1,11),knn_s,label="KNN")
   plt.legend()
   plt.show()






运行结果如下图:

​​​​​​​随机森林和KNN分类结果可视化(Sklearn)_第1张图片

你可能感兴趣的:(机器学习,sklearn,随机森林,分类)