决策树和随机森林对比

这篇具有很好参考价值的文章主要介绍了决策树和随机森林对比。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.用accuracy来对比

# -*-coding:utf-8-*-

"""
accuracy来对比决策树和随机森林
"""
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine

#(178, 13)
wine=load_wine()
# print(wine.data.shape)
print(wine.target)
from sklearn.model_selection import train_test_split
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就是accuracy
score_c=clf.score(Xtest,Ytest)
score_rfc=rfc.score(Xtest,Ytest)

print("Single Tree:{}".format(score_c),
      "Random Forest:{}".format(score_rfc))


Single Tree:0.8703703703703703 Random Forest:1.0

2.交叉熵验证对比

# -*-coding:utf-8-*-
"""
交叉熵来对比决策树和随机森林
"""
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
plt.switch_backend("TkAgg")
#(178, 13)
wine=load_wine()
# print(wine.data.shape)
print(wine.target)
from sklearn.model_selection import train_test_split
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)

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="DecisionTree")
plt.legend()
plt.show()

决策树和随机森林对比,机器学习,决策树,随机森林,深度学习

 3.多次平均交叉熵对比

# -*-coding:utf-8-*-

"""
交叉熵平均来对比决策树和随机森林
"""
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
plt.switch_backend("TkAgg")
#(178, 13)
wine=load_wine()
# print(wine.data.shape)
print(wine.target)
from sklearn.model_selection import train_test_split
Xtrain,Xtest,Ytrain,Ytest=train_test_split(wine.data,wine.target,test_size=0.3)

rfc_mc=[]
clf_mc=[]

for i in range(10):
    rfc=RandomForestClassifier(n_estimators=25)
    rfc_s=cross_val_score(rfc,wine.data,wine.target,cv=10).mean()
    rfc_mc.append(rfc_s)

    clf=DecisionTreeClassifier()
    clf_s=cross_val_score(clf,wine.data,wine.target,cv=10).mean()
    clf_mc.append(clf_s)

plt.plot(range(1,11),rfc_mc,label="Random Forest")
plt.plot(range(1,11),clf_mc,label="Decision Tree")
plt.legend()
plt.show()

决策树和随机森林对比,机器学习,决策树,随机森林,深度学习

 4.选择合适的estimators

为随机森林选择合适的决策树的数量

# -*-coding:utf-8-*-
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
plt.switch_backend("TkAgg")
#(178, 13)
wine=load_wine()
# print(wine.data.shape)
print(wine.target)
from sklearn.model_selection import train_test_split
Xtrain,Xtest,Ytrain,Ytest=train_test_split(wine.data,wine.target,test_size=0.3)

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))+1)
plt.figure(figsize=[20,5])
plt.plot(range(1,201),superpa)
plt.show()
0.9888888888888889 26

 决策树和随机森林对比,机器学习,决策树,随机森林,深度学习

 文章来源地址https://www.toymoban.com/news/detail-640643.html

到了这里,关于决策树和随机森林对比的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包