代码如下:
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()
运行结果如下图:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/143111.html