问题描述
您好,我尝试使用我的 csv 数据表创建决策树.我使用以下命令在 anaconda 和 python 中安装了 graphviz 包:
Hallo I try to create a decisiontree with my csv datasheet. I installed in anaconda and python the graphviz package with the following command:
conda install graphviz
pip install graphviz
让我的树可见.这是我在 Jupyther Notebook 中编写的代码:
to get my tree visible. Here is my code that I have wrote in Jupyther Notebook:
import pandas as pd
import graphviz
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split
file = 'automotive_data.csv'
COLS = np.arange(0,22,1).tolist()#gibt später bei usecols eine andere möglichkeit die spalten anzusprechen
data = pd.read_csv(file, header=0, sep = ",", index_col=0, usecols=COLS)
x = data.iloc[:,1:]
x = x.to_numpy()
y = data[['Ausfall']]
y
xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size=0.3, random_state=1)
model = DecisionTreeClassifier (
criterion='entropy',
splitter='best',
min_samples_split= 0.3,
max_features=10,
max_depth=None
)
#Danach mit fit erstellt
model.fit(xTrain, yTrain)
dot=export_graphviz(model, out_file=None,filled=True,
feature_names=data.columns[1:24],
class_names=['ja','nein']);
# Erzeuge Graphviz-Graphen aus dot-Quellcode
graph = graphviz.Source(dot)
graph#Here I get an error
在最后一行我得到了错误:
In the last row I get the error:
Format: "svg" not recognized. Use one of:
CalledProcessError Traceback (most recent call last)
~anaconda3libsite-packagesIPythoncoreformatters.py in __call__(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
~anaconda3libsite-packagesgraphvizfiles.py in _repr_svg_(self)
111
112 def _repr_svg_(self):
--> 113 return self.pipe(format='svg').decode(self._encoding)
114
115 def pipe(self, format=None, renderer=None, formatter=None, quiet=False):
~anaconda3libsite-packagesgraphvizfiles.py in pipe(self, format, renderer, formatter, quiet)
136 out = backend.pipe(self._engine, format, data,
137 renderer=renderer, formatter=formatter,
--> 138 quiet=quiet)
139
140 return out
~anaconda3libsite-packagesgraphvizackend.py in pipe(engine, format, data, renderer, formatter, quiet)
242 """
243 cmd, _ = command(engine, format, None, renderer, formatter)
--> 244 out, _ = run(cmd, input=data, capture_output=True, check=True, quiet=quiet)
245 return out
246
~anaconda3libsite-packagesgraphvizackend.py in run(cmd, input, capture_output, check, encoding, quiet, **kwargs)
182 if check and proc.returncode:
183 raise CalledProcessError(proc.returncode, cmd,
--> 184 output=out, stderr=err)
185
186 return out, err
CalledProcessError: Command '['dot', '-Tsvg']' returned non-zero exit status 1. [stderr: b'Format: "svg" not recognized. Use one of:
']
我也尝试使用 PNG 作为我的格式,但它也不起作用.我不知道如何解决这个问题.
I also tried to use PNG as my format but it didn't work too. I have no idea how to solve this problem.
推荐答案
显然问题是你必须先配置 graphviz 插件.
So apparently the issue is you have to configure the graphviz plugins first.
以管理员模式打开终端并运行dot -c
.(这假设 graphviz 二进制文件在您的路径中)
Open a terminal in administrator mode and run dot -c
. (This assumes that the graphviz binaries are in your path)
这篇关于错误:格式:“svg"未能识别.使用以下之一:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!