决策树(Decision Tree)
通过sklearn库的决策树模型对iris数据进行多分类,并进行结果评估
导入:

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn import datasets
from sklearn.datasets import load_breast_cancer
from sklearn import tree
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from sklearn.model_selection import KFold
import sklearn

加载数据:

# 加载iris数据
iris = load_iris()
# x,y数据赋值
x, y = iris.data, iris.targetprint('x:', x.shape)
print(x[:10,:])
print('y:', y.shape)
print(y)

运行结果如下:

x: (150, 4)
[[5.1 3.5 1.4 0.2][4.9 3.  1.4 0.2][4.7 3.2 1.3 0.2][4.6 3.1 1.5 0.2][5.  3.6 1.4 0.2][5.4 3.9 1.7 0.4][4.6 3.4 1.4 0.3][5.  3.4 1.5 0.2][4.4 2.9 1.4 0.2][4.9 3.1 1.5 0.1]]
y: (150,)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2]

使用sklearn库对iris数据集进行乱序切分为训练集和测试集(7:3比例),并使用决策树模型对测试集进行分类,最后对分类结果的准确率、召回率进行评估:

from sklearn.model_selection import train_test_splitx_train,x_test,y_train,y_test= train_test_split(iris.data, iris.target, test_size=0.3, random_state=0)breast_cancer = load_breast_cancer()
data = breast_cancer.data
target = breast_cancer.target
target_names = ['class train', 'class test']
clf = tree.DecisionTreeClassifier(random_state=0)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)# print(confusion_matrix(y_test, y_pred, labels=[1, 0]))        # 混淆矩阵
print(classification_report(y_test, y_pred, target_names=target_names,labels=[1, 0]))
# 显示主要分类指标的文本报告

运行结果如下:

             precision    recall  f1-score   supportclass train       1.00      0.94      0.97        18class test       1.00      1.00      1.00        16avg / total       1.00      0.97      0.98        34

使用sklearn库的决策树模型对iris数据集进行10折交叉验证,评估每折的正确率,并计算平均准确率:

from sklearn.model_selection import cross_val_score
import numpy as np
d_scores = cross_val_score(DecisionTreeClassifier(), iris.data, iris.target, cv=10)for i in range(len(d_scores)):print("第",i+1,"折的准确率 ",format(d_scores[i],'.4f'))print("平均准确率:    ",np.average(d_scores))

运行结果如下:

第 1 折的准确率  1.0000
第 2 折的准确率  0.9333
第 3 折的准确率  1.0000
第 4 折的准确率  0.9333
第 5 折的准确率  0.9333
第 6 折的准确率  0.8667
第 7 折的准确率  0.9333
第 8 折的准确率  1.0000
第 9 折的准确率  1.0000
第 10 折的准确率  1.0000
平均准确率:     0.96

修改决策树模型中的参数(如criterion、max_depth、spliter等)评估10折交叉验证下的平均准确率,至少验证4组不同参数的决策树模型:

Dy = []
kf=KFold(n_splits=10,shuffle=True,random_state=True)x=iris['data']
kiss = DecisionTreeClassifier(criterion='entropy', max_depth=30, splitter='best')
Accuracy=0
for train_index, test_index in kf.split(x):x_train, x_test = x[train_index], x[test_index]y_train, y_test = y[train_index], y[test_index]clf2 = kiss.fit(x_train, y_train)pre = clf2.predict(x_test)This_Time = accuracy_score(y_test, pre, normalize = True )Accuracy+=This_TimeDy.append(This_Time)
print("第一次准确率",Accuracy/10)kiss = DecisionTreeClassifier(criterion='gini', max_depth=20, splitter='random')
Accuracy=0
for train_index, test_index in kf.split(x):x_train, x_test = x[train_index], x[test_index]y_train, y_test = y[train_index], y[test_index]clf2 = kiss.fit(x_train, y_train)pre = clf2.predict(x_test)This_Time = accuracy_score(y_test, pre, normalize = True )Accuracy+=This_TimeDy.append(This_Time)
print("第二次准确率",Accuracy/10)kiss = DecisionTreeClassifier(criterion='entropy', max_depth=20, splitter='random')
Accuracy=0
for train_index, test_index in kf.split(x):x_train, x_test = x[train_index], x[test_index]y_train, y_test = y[train_index], y[test_index]clf2 = kiss.fit(x_train, y_train)pre = clf2.predict(x_test)This_Time = accuracy_score(y_test, pre, normalize = True )Accuracy+=This_TimeDy.append(This_Time)
print("第三次准确率",Accuracy/10)kiss = DecisionTreeClassifier(criterion='entropy', max_depth=30, splitter='random')
Accuracy=0
for train_index, test_index in kf.split(x):x_train, x_test = x[train_index], x[test_index]y_train, y_test = y[train_index], y[test_index]clf2 = kiss.fit(x_train, y_train)pre = clf2.predict(x_test)This_Time = accuracy_score(y_test, pre, normalize = True )Accuracy+=This_TimeDy.append(This_Time)
print("第四次准确率",Accuracy/10)

运行结果如下:

第一次准确率 0.9600000000000002
第二次准确率 0.9200000000000002
第三次准确率 0.9466666666666669
第四次准确率 0.9466666666666669

python与机器学习(五)——决策树相关推荐

  1. 机器学习(五)决策树(decision tree)

    决策树(decision tree)(一)--构造决策树方法 决策树算法起源于E.B.Hunt等人于1966年发表的论文"experiments in Induction",但真正 ...

  2. python神经网络算法pdf_Python与机器学习实战 决策树、集成学习、支持向量机与神经网络算法详解及编程实现.pdf...

    作 者 :何宇健 出版发行 : 北京:电子工业出版社 , 2017.06 ISBN号 :978-7-121-31720-0 页 数 : 315 原书定价 : 69.00 主题词 : 软件工具-程序设计 ...

  3. 机器学习实战-决策树(二)Python实现

    转载请注明作者和出处: http://blog.csdn.net/c406495762 运行平台: Windows Python版本: Python3.x IDE: Sublime text3 一 前 ...

  4. python分类算法报告_Python机器学习(1)——决策树分类算法

    1.决策树算法 决策树用树形结构对样本的属性进行分类,是最直观的分类算法,而且也可以用于回归.不过对于一些特殊的逻辑分类会有困难.典型的如异或(XOR)逻辑,决策树并不擅长解决此类问题. 决策树的构建 ...

  5. 【机器学习基础】数学推导+纯Python实现机器学习算法4:决策树之ID3算法

    Python机器学习算法实现 Author:louwill 作为机器学习中的一大类模型,树模型一直以来都颇受学界和业界的重视.目前无论是各大比赛各种大杀器的XGBoost.lightgbm还是像随机森 ...

  6. 【机器学习基础】数学推导+纯Python实现机器学习算法5:决策树之CART算法

    目录 CART概述 回归树 分类树 剪枝 Python实现示例:分类树 在数学推导+纯Python实现机器学习算法4:决策树之ID3算法中笔者已经对决策树的基本原理进行了大概的论述.本节将在上一讲的基 ...

  7. 模式识别与机器学习(Python实现):决策树分男女

    模式识别与机器学习(Python实现):决策树分男女 欢迎大家来到安静到无声的<模式识别与人工智能(程序与算法)>,如果对所写内容感兴趣请看模式识别与人工智能(程序与算法)系列讲解 - 总 ...

  8. 元旦福利 | Python、机器学习、TensorFlow 图书送一波

    随着人工智能的升温,大家越来越关注这门最接近AI的编程语言,关于Python的消息也是接连不断.比如浙江省信息技术课程改革方案出台,Python 确定进入浙江省信息技术高考,从2018年起浙江省信息技 ...

  9. 零基础该如何系统地自学Python编程?五个阶段带你从小白到大佬

    对于零基础学习或是已经学完基础不知道下一步该干什么的朋友,可以看看这篇缓解迷茫.今天分享下如何系统地自学Python规划目标,有一个学习目标在去行动. 有了目标,怎么行动呢?建议采用视频+书籍的方式进 ...

  10. 【机器学习基础】数学推导+纯Python实现机器学习算法24:LightGBM

    Python机器学习算法实现 Author:louwill Machine Learning Lab 第17讲我们谈到了竞赛大杀器XGBoost,本篇我们来看一种比XGBoost还要犀利的Boosti ...

最新文章

  1. java arraylist 对象 删除_ArrayList实现删除重复元素(元素不是对象类型的情况)...
  2. linux选择最短路径sdn,基于网络流量的SDN最短路径转发应用
  3. 运维大数据可视化分析平台来了,枯燥运维数据也可以生动起来
  4. matlab工具包pls,MATLAB PLS_tools PLS部分最小二乘工具箱(主程序)挺好用的数据分析程序 - 下载 - 搜珍网...
  5. easyui数据表格重置_数据库三种删除方式
  6. 密码学基本概念(一)
  7. [转]Eclipse工具使用技巧总结
  8. 【代码笔记】iOS-竖状图
  9. JAVA小项目--银行管理系统(GUI+数据库mysql)
  10. plc原理及应用_【工控资料】西门子、三菱、欧姆龙PLC电气设计与编程自学宝典(双色版)...
  11. 负载均衡添加ssl证书
  12. 用友U8案例教程成本报表
  13. H.266/VVC技术学习:帧内预测之MIP技术
  14. [java]如何在项目中用好log4J写项目日志
  15. 计算机二级两个控件之间求偶,求,全国计算机等级考试二级java历年试题及答案合集,还有上机考试真题?...
  16. 把吃出来的病吃回去 张悟本_吃出来的华为
  17. java jlabel图片大小_java – 调整图片大小以适应JLabel
  18. STM32F767 使用I2C驱动DW9714,控制VCM音圈电机位移
  19. Hexo博客优化:在Next主题中设置进阶版Live2D看板娘————拒绝踩坑!!!!
  20. 网易技术:手游频繁崩溃”闪退”的原因是什么?

热门文章

  1. 操作class属性的新API--classList
  2. V4L2学习(三)框架分析
  3. 11 个 Git 面试题
  4. eclipse 报错问题:java.lang.ClassNotFoundException:
  5. Window命令行工具操作文件
  6. node之npm一直出错
  7. 将Linux代码移植到Windows的简单方法
  8. 不用也要知道的几种算法(PHP版本)
  9. 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目1
  10. AutoIt: send 命令 VS ControlClick的使用