使用python创建决策树

时间:2018-03-26 21:06:37

标签: python pandas jupyter graphviz

我正在使用名为“wine”的数据集创建决策树: 我正在尝试执行以下代码:

dt = c.fit(X_train, y_train)

创建决策树的图像:

其中“Malik Shahid Ali”是图像的位置/路径

def show_tree(tree, features, path):
    f = io.StringIO()
    export_graphviz(tree, out_file=f, feature_names=features)
    pydotplus.graph_from_dot_data(f.getvalue()).write_png("Malik Shahid Ali")
    img = misc.imread("Malik Shahid Ali")
    plt.imshow(img)

调用图像:

show_tree(dt, features, 'dec_tree_01.png')

但是当我调用图像时会出现以下错误:

GraphViz's executables not found

导入部分:

import numpy as np
import pandas as pd
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split
import graphviz
import pydotplus
import io
from scipy import misc
import matplotlib.pyplot as plt #sets up plotting under plt
import seaborn as sb
from pylab import rcParams

阅读csv数据集

data=pd.read_csv('C:/Users/malik/Desktop/wine.csv',low_memory=False)
data.head()
train, test = train_test_split(data,test_size=0.15)
print("Training size: {} Test size: {}".format(len(train),len(test)))
c=DecisionTreeClassifier(min_samples_split=2)
features = ["id","Alcohol","Malic acid","Ash","Alcalinity of ash","Magnesium","Total phenols","Flavanoids","Field9Nonflavanoid phenols","Proanthocyanins","Color intensity","Hue","OD280/OD315 of diluted wines","Proline"]
X_train = train[features]
y_train = train["id"]

X_test = test[features]
y_test = test["id"]
y_test
dt = c.fit(X_train, y_train)

可删除文件的路径:

import os     
os.environ["PATH"] += os.pathsep + 'E:\Graphviz2.38\bin'

图像功能:

def show_tree(tree, features, path):
    f = io.StringIO()
    export_graphviz(tree, out_file=f, feature_names=features)
    pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)
    img = misc.imread(path)

    plt.imshow(img)
show_tree(dt, features, 'dec_tree_01.png')

现在,在这个命令中,jupyter正在给出这样的错误:

E:\python\lib\site-packages\pydotplus\graphviz.py in create(self, prog, format)
   1958             if self.progs is None:
   1959                 raise InvocationException(
-> 1960                     'GraphViz\'s executables not found')
   1961 
   1962         if prog not in self.progs:

InvocationException: GraphViz's executables not found

1 个答案:

答案 0 :(得分:0)

我正在重新解决相关问题的答案here

确保已安装实际的可执行文件,而不仅仅是python包。我使用了conda的安装包here(建议超过pip install graphviz因为pip安装没有     包括实际的GraphViz executables)。

更新

在一天结束时,将未正确格式化的必要文件目录的字符串路径添加到环境变量PATH中。一定要在目录的字符串路径中添加双反斜杠,例如:

import os
os.environ["PATH"] += os.pathsep + 'E:\\Graphviz2.38\\bin\\'