# coding=utf-8

 import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.metrics import precision_recall_curve #准确率与召回率
import numpy as np
#import graphviz import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' def get_data():
file_path = "Iris.xlsx" data = pd.read_excel(file_path)
loandata = pd.DataFrame(data)
ncol = (len(loandata.keys()))
print(ncol)
# l = list(data.head(0)) #获取表头
# print(l) feature1 = []
for i in range(ncol-1):
feature1.append("feature"+str(i))
print(feature1)
iris_x = data.iloc[1:, :ncol-1]#此处有冒号,不显示最后一列
iris_y = data.iloc[1:,ncol-1]#此处没有冒号,直接定位 '''计算到底有几个类别'''
from collections import Counter
counter = Counter(iris_y)
con = len(counter)
print(counter.keys())
class_names = []
for i in range(con):
class_names.append(list(counter.keys())[i])
x_train, x_test, y_train, y_test = train_test_split(iris_x,iris_y)
print(x_train)
print(y_test)
# return x_train, x_test, y_train, y_test #def dtfit(x_train, x_test, y_train, y_test): clf = tree.DecisionTreeClassifier()
clf = clf.fit(x_train,y_train)
predict_data = clf.predict(x_test)
predict_proba = clf.predict_proba(x_test)
from sklearn import metrics
# Do classification task,
# then get the ground truth and the predict label named y_true and y_pred
classify_report = metrics.classification_report(y_test, clf.predict(x_test))
confusion_matrix = metrics.confusion_matrix(y_train, clf.predict(x_train))
overall_accuracy = metrics.accuracy_score(y_train, clf.predict(x_train))
acc_for_each_class = metrics.precision_score(y_train,clf.predict(x_train), average=None)
overall_accuracy = np.mean(acc_for_each_class)
print(classify_report) import pydotplus
dot_data = tree.export_graphviz(clf, out_file=None,feature_names=feature1, filled=True, rounded=True, special_characters=True,precision = 4)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("workiris.pdf")
return classify_report if __name__ == "__main__":
x = get_data()
#dtfit(x_train, x_test, y_train, y_test)

数据地址:http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data

保存后注意填写表头

iris数据集 决策树实现分类并画出决策树的更多相关文章

  1. scikit-learn机器学习(四)使用决策树做分类,并画出决策树,随机森林对比

    数据来自 UCI 数据集 匹马印第安人糖尿病数据集 载入数据 # -*- coding: utf-8 -*- import pandas as pd import matplotlib matplot ...

  2. ROC曲线是通过样本点分类概率画出的 例如某一个sample预测为1概率为0.6 预测为0概率0.4这样画出来,此外如果曲线不是特别平滑的话,那么很可能存在过拟合的情况

    ROC和AUC介绍以及如何计算AUC from:http://alexkong.net/2013/06/introduction-to-auc-and-roc/ ROC(Receiver Operat ...

  3. Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型的准确率(图文详解)

    不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的 ...

  4. 85、使用TFLearn实现iris数据集的分类

    ''' Created on 2017年5月21日 @author: weizhen ''' #Tensorflow的另外一个高层封装TFLearn(集成在tf.contrib.learn里)对训练T ...

  5. Iris数据集实战

    本次主要围绕Iris数据集进行一个简单的数据分析, 另外在数据的可视化部分进行了重点介绍. 环境 win8, python3.7, jupyter notebook 目录 1. 项目背景 2. 数据概 ...

  6. javascript实现朴素贝叶斯分类与决策树ID3分类

    今年毕业时的毕设是有关大数据及机器学习的题目.因为那个时间已经步入前端的行业自然选择使用JavaScript来实现其中具体的算法.虽然JavaScript不是做大数据处理的最佳语言,相比还没有优势,但 ...

  7. 机器学习笔记2 – sklearn之iris数据集

    前言 本篇我会使用scikit-learn这个开源机器学习库来对iris数据集进行分类练习. 我将分别使用两种不同的scikit-learn内置算法--Decision Tree(决策树)和kNN(邻 ...

  8. CART决策树(分类回归树)分析及应用建模

    一.CART决策树模型概述(Classification And Regression Trees)   决策树是使用类似于一棵树的结构来表示类的划分,树的构建可以看成是变量(属性)选择的过程,内部节 ...

  9. 利用ggplot2画出各种漂亮图片详细教程

    1.Why use ggplot2 ggplot2是我见过最human friendly的画图软件,这得益于Leland Wilkinson在他的著作<The Grammar of Graphi ...

随机推荐

  1. mysql一致性读

    Consistent Nonlocking Reads 一致读意味着InnoDB用多版本来提供一个查询数据库某个时间点的快照.这种查询可以看到在当前世界点之前事务提交的改变,看不到此后提交的改变,更看 ...

  2. Scala的安装,入门,学习,基础

    1:Scala的官方网址:http://www.scala-lang.org/ 推荐学习教程:http://www.runoob.com/scala/scala-tutorial.html Scala ...

  3. VS Code 快捷键(中英文对照版)

    原文地址:https://segmentfault.com/a/1190000007688656 常用 General 按 Press 功能 Function Ctrl + Shift + P,F1 ...

  4. Effective Java 第三版——19. 如果使用继承则设计,并文档说明,否则不该使用

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  5. T4模板生成代码。 数据实体层与数据仓储层。备注

    文件生成模板:TempleteManager.ttinclude <#@ assembly name="System.Core" #><#@ assembly n ...

  6. git遇到的问题解决方案

    问题1: The requested URL returned error: 401 Unauthorized while accessing? 问题原因: 1.无法访问是因为你将项目设置为私人的,所 ...

  7. 新浪新闻页面抓取(JAVA-Jsoup)

    1.使用gradle建立工程: 工程格式如下: include ':spider-demo' rootProject.name = 'my-spider-demo' settings def void ...

  8. BZOJ2425: [HAOI2010]计数

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2425 其实能够构成的数就是原数的排列(算前导0),然后组合计数一下就可以了. #include ...

  9. Graph(Floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  10. chrome浏览器使用技巧

    在学校的时候一直在用firefox火狐浏览器,听一个学长说使用chrome浏览器在面试的时候有加分,而且还跟我说了一些chrome浏览器的使用技巧,最后从火狐浏览器转到谷歌浏览器,就一直在使用谷歌浏览 ...