我正在尝试在python中构建一个决策树并将其可视化。然而,graphviz正在产生一个问题。
这是我尝试过的代码:
# -*- coding: utf-8 -*-
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import os
import matplotlib.pylab as plt
import sklearn
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
import sklearn.metrics
from sklearn.metrics import classification_report
from sklearn import tree
from io import BytesIO as StringIO
from IPython.display import Image
import pydot
import pydotplus
import graphviz
path = "C:\\Users\\admin\\PycharmProjects\\conference\\New\\decision_tree\\"
data = pd.read_csv(path + "weights.csv")
data_clean = data.dropna()
#data_clean.dtypes()
#data_clean.describe()
predictors = data_clean[['anxiety', 'taking']]
target = data_clean['influence_score']
pred_train, pred_test, tar_train, tar_test = train_test_split(predictors,target, test_size=.2)
#pred_train.shape
#pred_test.shape
#tar_test.shape
#tar_train.shape
classifier = DecisionTreeClassifier()
classifier = classifier.fit(pred_train,tar_train)
predictons = classifier.predict(pred_test)
sklearn.metrics.confusion_matrix(tar_test,predictons)
data = StringIO()
#out_dot = path + "tree.dot"
tree.export_graphviz(classifier, out_file = data)
graph = pydotplus.graph_from_dot_data(data.getvalue())
out_tree = path + "tree_full.pdf"
graph.write_pdf(out_tree)
但是我一直收到这个错误。
File "C:\Python27\lib\site-packages\pydotplus\graphviz.py", line 1960, in create
'GraphViz\'s executables not found')
pydotplus.graphviz.InvocationException: GraphViz's executables not found
我已经按照安装Graphviz
和pydotplus
的所有步骤进行了操作,包括更改环境变量以及在python IDE中导入它们(我使用的是pycharm)。 Graphviz版本安装2.38。
我如何使这项工作。
答案 0 :(得分:0)
我解决了。似乎我在更新环境变量时犯了一个错误。工作正常。