graphviz数据可视化 与Python交互
2017-06-30 11:32
459 查看
graphviz
1下载
下载链接: http://www.graphviz.org/Download_windows.php选择 graphviz-2.38.msi
2安装
下一步 - 下一步3环境配置
将graphviz下的bin安装路径 放到 系统环境变量中 ,4测试安装成功
cmd 命令行输入 dot -version
出现版本信息 ,说明配置成功
Python
Python下生成的tree.dot
1在python工程文件中放1.txt
1.txt内容
1.5 50 thin
1.5 60 fat
1.6 40 thin
1.6 60 fat
1.7 60 thin
1.7 80 fat
1.8 60 thin
1.8 90 fat
1.9 70 thin
1.9 80 fat
2决策树生成代码,生成一个新的文件 tree.dot
# -*- coding: utf-8 -*-import numpy as np
import scipy as sp
from sklearn import tree
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import classification_report
from sklearn.cross_validation import train_test_split
''''' 数据读入 '''
data = []
labels = []
with open("1.txt") as ifile:
for line in ifile:
tokens = line.strip().split(' ')
data.append([float(tk) for tk in tokens[:-1]])
labels.append(tokens[-1])
x = np.array(data)
labels = np.array(labels)
y = np.zeros(labels.shape)
''''' 标签转换为0/1 '''
y[labels == 'fat'] = 1
''''' 拆分训练数据与测试数据 '''
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
''''' 使用信息熵作为划分标准,对决策树进行训练 '''
clf = tree.DecisionTreeClassifier(criterion='entropy')
print(clf)
clf.fit(x_train, y_train)
''''' 把决策树结构写入文件 '''
with open("tree.dot", 'w') as f:
f = tree.export_graphviz(clf, out_file=f)
''''' 系数反映每个特征的影响力。越大表示该特征在分类中起到的作用越大 '''
print(clf.feature_importances_)
'''''测试结果的打印'''
answer = clf.predict(x_train)
print(x_train)
print(answer)
print(y_train)
print(np.mean(answer == y_train))
'''''准确率与召回率'''
precision, recall, thresholds = precision_recall_curve(y_train, clf.predict(x_train))
answer = clf.predict_proba(x)[:, 1]
print(classification_report(y, answer, target_names=['thin', 'fat']))
tree.dot
digraph Tree {
node [shape=box] ;
0 [label="X[1] <= 75.0\nentropy = 1.0\nsamples= 8\nvalue = [4, 4]"] ;
1 [label="X[0] <= 1.65\nentropy =0.9183\nsamples = 6\nvalue = [4, 2]"] ;
0 -> 1 [labeldistance=2.5, labelangle=45,headlabel="True"] ;
2 [label="X[1] <= 50.0\nentropy =0.9183\nsamples = 3\nvalue = [1, 2]"] ;
1 -> 2 ;
3 [label="entropy = 0.0\nsamples = 1\nvalue = [1,0]"] ;
2 -> 3 ;
4 [label="entropy = 0.0\nsamples = 2\nvalue = [0,2]"] ;
2 -> 4 ;
5 [label="entropy = 0.0\nsamples = 3\nvalue = [3,0]"] ;
1 -> 5 ;
6 [label="entropy = 0.0\nsamples = 2\nvalue = [0,2]"] ;
0 -> 6 [labeldistance=2.5, labelangle=-45,headlabel="False"] ;
}
3在命令行 cd 定位到tree.dot的文件位置
输入 dot -Tpdf tree.dot -o tree.pdf 或者 dot -Tpng tree.dot -o tree.png 分别生成对应的pdf格式文件 或者 png图片格式4生成tree.pdf文件
相关文章推荐
- python的数据可视化 graphviz pydot安装配置(win10)
- python的数据可视化 graphviz pydot安装配置(win10)
- python的数据可视化 graphviz 安装配置
- 读书笔记--python数据可视化--007_清理异常值
- 在Python中对MySQL中的数据进行可视化
- 在Python中对MySQL中的数据进行可视化
- Python脚本调用C#代码数据交互示例(hello world)
- 读书笔记--python数据可视化--005_从数据库导入数据
- Python数据分析及可视化的基本环境
- Python数据可视化——散点图
- 读书笔记--python数据可视化--003_读取定宽数据文件
- simulink与python进行数据交互
- python数据可视化--散点图以及分类
- 读书笔记--python数据可视化--004_读取JSON数据
- 7种python 可视化 库的比较(针对同一数据画图)
- 交互设计、信息图、信息可视化、数据可视化技术资源汇总——设计师的领域,设计师说了算
- 数据可视化-Python之Matplotlib
- d3.js:数据可视化利器之 交互行为:响应DOM事件
- 读书笔记--python数据可视化--002_读取Excel文件数据