决策树

决策树

    正好,我们专业数学基础与机器学习这两门课都有关于决策树的内容。在本篇文
章中,主要是讲最普通的决策树———基于信息增益的决策树。
     首先介绍一下决策树与算法步骤;之后为方便理解,给个例题简单算一算;最
后,用Python自带的包,简单在电脑上实现一下。

1、简单介绍与算法步骤:
在这里插入图片描述
2、例题:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
3、Python实现:

   注:我这个数据集用的是Python自带的莺尾花数据集,而且如果想要可视化的话
,需要安装pydotplus与graphviz环境。
   代码如下:
from sklearn import tree#决策树分类器
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris #导入数据集
import pydotplus
import os
os.environ["PATH"] += os.pathsep + 'E:/graphviz/bin'
iris = load_iris()
# 解析数据
x=iris.data
y=iris.target.reshape(-1,1)
# 划分训练集和测试集
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)
module = tree.DecisionTreeClassifier(criterion='gini',max_depth=3)
module.fit(x_train, y_train.ravel())
dot_data = tree.export_graphviz(module, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("iris3.pdf")
Z = module.predict(x_test)
plt.plot(x_test[0:45],y_test[0:45],c = 'b')
plt.plot(x_test[0:45],Z[0:45],c = 'r')
plt.show()

   步骤与结果如下:

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值