from sklearn.model_selection import train_test_split
#划分30%的数据作为测试集
Xtrain, Xtest, Ytrain, Ytest = train_test_split(wine.data, wine.target, test_size=0.3)
clf = DecisionTreeClassifier(random_state=0) #建立模型: 决策树
rfc = RandomForestClassifier(random_state=0) #建立模型: 随机森林
clf = clf.fit(Xtrain, Ytrain) # 训练模型: 决策树
rfc = rfc.fit(Xtrain, Ytrain) # 训练模型: 随即森林
score_c = clf.score(Xtest, Ytest) #返回预测的准确度: 随机森林
score_r = rfc.score(Xtest, Ytest) #返回预测的准确度: 决策树
# 打印出准确度
print("single Tree:{}".format(score_c),
"Random Forest:{}".format(score_r))
single Tree:0.9444444444444444 Random Forest:0.9814814814814815
可见随机森林默认就比决策树准确度要高。
**4、画出随机森林和决策树在一组交叉验证下的效果对比**
> **交叉验证**
> 将数据集划分为n份,依次取每一份做测试集,每n-1份做训练集,多次训练模型以观测模型稳定性的方法
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
随机森林
rfc = RandomForestClassifier(n_estimators=25)
rfc_s = cross_val_score(rfc, wine.data, wine.target, cv=10)
决策树
clf = DecisionTreeClassifier()
clf_s = cross_val_score(clf, wine.data, wine.target, cv=10)
plt.plot(range(1,11), rfc_s, label = “RandomForest”)
plt.plot(range(1,11), clf_s, label = “Decision Tree”)
plt.legend()
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210211213139202.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
另一种更加简单有趣的写法:
label = “RandomForest”
for model in [RandomForestClassifier(n_estimators=25),DecisionTreeClassifier()]:
score = cross_val_score(model,wine.data, wine.target, cv=10)
print("{}:".format(label)),print(score.mean())
plt.plot(range(1,11),score,label = label)
plt.legend()
label = "DecisionTree"
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210211213509450.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
**5、画出随机森林和决策树在十组交叉验证下的效果对比**
rfc_l = []
clf_l = []
for i in range(10):
rfc = RandomForestClassifier(n_estimators=25)
rfc_s = cross_val_score(rfc,wine.data,wine.target,cv=10).mean()
rfc_l.append(rfc_s)
clf = DecisionTreeClassifier()
clf_s = cross_val_score(clf,wine.data,wine.target,cv=10).mean()
clf_l.append(clf_s)
plt.plot(range(1,11), rfc_l, label=“Random Forest”)
plt.plot(range(1,11), clf_l, label=“Decision Tree”)
plt.legend()
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021021122074319.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
**6、n\_estimators的学习曲线**
#####【TIME WARNING: 2mins 30 seconds】#####
superpa = []
for i in range(200):
rfc = RandomForestClassifier(n_estimators=i+1,n_jobs=-1)
rfc_s = cross_val_score(rfc,wine.data,wine.target,cv=10).mean()
superpa.append(rfc_s)
print(max(superpa), superpa.index(max(superpa))) # 0.9888888888888889 53
plt.figure(figsize=[20,5])
plt.plot(range(1,201),superpa)
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210211221112484.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,