使用python机器学习怎么实现决策树
发表于:2023-03-24 作者:安全数据网编辑
编辑最后更新 2023年03月24日,这期内容当中小编将会给大家带来有关使用python机器学习怎么实现决策树,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。具体内容如下# -*- coding: utf
这期内容当中小编将会给大家带来有关使用python机器学习怎么实现决策树,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
具体内容如下
# -*- coding: utf-8 -*-"""Created on Sat Nov 9 10:42:38 2019@author: asus""""""决策树目的:1. 使用决策树模型2. 了解决策树模型的参数3. 初步了解调参数要求:基于乳腺癌数据集完成以下任务:1.调整参数criterion,使用不同算法信息熵(entropy)和基尼不纯度算法(gini)2.调整max_depth参数值,查看不同的精度3.根据参数criterion和max_depth得出你初步的结论。"""import matplotlib.pyplot as pltimport numpy as npimport pandas as pdimport mglearn from sklearn.model_selection import train_test_split#导入乳腺癌数据集from sklearn.datasets import load_breast_cancerfrom sklearn.tree import DecisionTreeClassifier#决策树并非深度越大越好,考虑过拟合的问题#mglearn.plots.plot_animal_tree()#mglearn.plots.plot_tree_progressive()#获取数据集cancer = load_breast_cancer()#对数据集进行切片X_train,X_test,y_train,y_test = train_test_split(cancer.data,cancer.target, stratify = cancer.target,random_state = 42)#查看训练集和测试集数据 print('train dataset :{0} ;test dataset :{1}'.format(X_train.shape,X_test.shape))#建立模型(基尼不纯度算法(gini)),使用不同最大深度和随机状态和不同的算法看模型评分tree = DecisionTreeClassifier(random_state = 0,criterion = 'gini',max_depth = 5)#训练模型tree.fit(X_train,y_train)#评估模型print("Accuracy(准确性) on training set: {:.3f}".format(tree.score(X_train, y_train)))print("Accuracy(准确性) on test set: {:.3f}".format(tree.score(X_test, y_test)))print(tree)# 参数选择 max_depth,算法选择基尼不纯度算法(gini) or 信息熵(entropy)def Tree_score(depth = 3,criterion = 'entropy'): """ 参数为max_depth(默认为3)和criterion(默认为信息熵entropy), 函数返回模型的训练精度和测试精度 """ tree = DecisionTreeClassifier(criterion = criterion,max_depth = depth) tree.fit(X_train,y_train) train_score = tree.score(X_train, y_train) test_score = tree.score(X_test, y_test) return (train_score,test_score)#gini算法,深度对模型精度的影响depths = range(2,25)#考虑到数据集有30个属性scores = [Tree_score(d,'gini') for d in depths]train_scores = [s[0] for s in scores]test_scores = [s[1] for s in scores]plt.figure(figsize = (6,6),dpi = 144)plt.grid()plt.xlabel("max_depth of decision Tree")plt.ylabel("score")plt.title("'gini'")plt.plot(depths,train_scores,'.g-',label = 'training score')plt.plot(depths,test_scores,'.r--',label = 'testing score')plt.legend()#信息熵(entropy),深度对模型精度的影响scores = [Tree_score(d) for d in depths]train_scores = [s[0] for s in scores]test_scores = [s[1] for s in scores]plt.figure(figsize = (6,6),dpi = 144)plt.grid()plt.xlabel("max_depth of decision Tree")plt.ylabel("score")plt.title("'entropy'")plt.plot(depths,train_scores,'.g-',label = 'training score')plt.plot(depths,test_scores,'.r--',label = 'testing score')plt.legend()
运行结果:
上述就是小编为大家分享的使用python机器学习怎么实现决策树了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
c语言网络技术和编程语言总结
c语言对应的三级考试网络技术
网站服务器后台如何加防护密码
c语言软件开发步骤
c语言中网络技术知识点
服务器对权限提升的防护措施
怎么在阿里云租服务器
黑苹果能做web服务器吗
c语言和网络技术哪个好考
云服务器2核4G是什么性能
小秘书下载软件开发
应用服务器是一个软件包对吗
网络技术可溯源
3代网络安全技术创新
饥荒里的服务器无应答
从特征上看网络安全除包含
软件开发公司的咨询费怎么做账
网络安全博览会的内容
小区物业网络安全操作规程
安装一个服务器多少钱
无线传感网络技术视频教程
上游服务器
网络安全的法律法规一览表
沈阳工业大学软件开发
华裔通讯网络技术有限公司
宜春手机软件开发招聘
山东易乐网络技术服务中心
服务器框架怎么下载
数据库的结构被分为哪三个级
聊城微信小程序软件开发哪家便宜
数据库中专门的数据运算有
ssd硬盘 服务器
方舟手游服务器变黄色
长沙市蕊聪网络技术有限责任公司
软件开发三驾马车
2003服务器杀毒
网络安全理论架构
楚雄互联网科技选哪家
虚拟主机 邮件服务器
黑客对数据库进行攻击最常用手段