I have a problem to create and display decision tree in Jupyter Notebook using Python. My code is as below:
X = data.drop(["Risk"], axis=1)
y = data["Risk"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
from sklearn.tree import DecisionTreeClassifier
klasyfikator = DecisionTreeClassifier(criterion = "gini", random_state=0, max_depth=4, min_samples_leaf=1)
klasyfikator.fit(X = X, y = y)
data = export_graphviz(klasyfikator,out_file=None,feature_names=X.columns,class_names=["0", "1"],
filled=True, rounded=True,
special_characters=True)
graph = graphviz.Source(data)
graph
Generally this decision tree concerns credit risk research 0 - will not pay 1 - will pay.
When I use code above, I have error like this:
ExecutableNotFound: failed to execute ['dot', '-Tsvg'], make sure the Graphviz executables are on your systems' PATH
I have already tried many solutions from StackOverflow, for example:
C:\\Program Files (x86)\\Graphviz2.38\\bin
And there is still error described above. What can I do? what should I do ? Please help me guys because I'm losing hope of being able to draw this tree. Thank you!
Moreover, when I added by using this code:
import os
os.environ["PATH"] += os.pathsep + 'C:\Program Files (x86)\Graphviz2.38\bin'
I have in PATH something like this: C:\\\\Program Files (x86)\\\\Graphviz2.38\\x08in
it is not the same, what can I do ?
With latest version of sklearn, you can directly plot the decision tree without graphviz
.
Use:
from sklearn.tree import plot_tree
plot_tree(klasyfikator)
Read more here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.