当前位置: 首页 > news >正文

决策树和随机森林对比

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

 

 

http://www.lryc.cn/news/113037.html

相关文章:

  • CS 144 Lab Seven -- putting it all together
  • opencv基础-29 Otsu 处理(图像分割)
  • gcc-buildroot-9.3.0 和 gcc-arm-10.3 的区别
  • IDEA Run SpringBoot程序步骤原理
  • 海康威视摄像头配置RTSP协议访问、onvif协议接入、二次开发SDK接入
  • Android中的Parcelable 接口
  • Docker-Compose编排与部署
  • Linux JDK 安装
  • JS中常用的数组拷贝技巧
  • SAP ABAP程序性能优化-养成良好的代码习惯
  • SQL SERVER ip地址改别名
  • 数据结构-1
  • Java自定义校验注解实现List、set集合字段唯一性校验
  • xiaoweirobot.chat
  • 【无公网IP】本地电脑搭建个人博客网站(并发布公网访问 )和web服务器
  • SpringCloud(29):Nacos简介
  • freeBSD - 笔记
  • 【Linux】网络基础——宏观认识计算机网络
  • 数字人现身大运会,怎么以动作捕捉技术助推运动与文博相结合
  • WSL安装
  • MongoDB 入门
  • 使用uni-app的uniCloud 云数据库入门:实现一个简单的增删改查
  • 【MATLAB第64期】【保姆级教程】基于MATLAB的SOBOL全局敏感性分析模型运用(含无目标函数,考虑代理模型)
  • Python web实战之Django用户认证详解
  • 每天五分钟机器学习:梯度下降算法和正规方程的比较
  • 生信学院|08月18日《基于Flow Simulation的冷链运输产品案例》
  • 不可错过的家装服务预约小程序商城开发指南
  • 任务 13、MidJourney种子激发极致创作,绘制震撼连贯画作
  • IAR开发环境的安装、配置和新建STM32工程模板
  • FPGA优质开源项目 – PCIE通信