我已经使用代码生成了一个点文件以可视化决策树
import numpy as np
from sklearn.model_selection import train_test_split
import sklearn.tree
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test =train_test_split(cancer.data,cancer.target, stratify=cancer.target, random_state=42)
tree = sklearn.tree.DecisionTreeClassifier(random_state=0,max_depth=4)
tree.fit(X_train,y_train)
sklearn.tree.export_graphviz(tree,out_file="tree.dot",class_names=cancer.target_names,feature_names=cancer.feature_names,impurity=False, filled=True)
这将成功创建tree.dot文件。我现在可以使用graphviz(https://graphviz.gitlab.io/_pages/Download/Download_windows.html)的dot.exe实用工具生成png文件。
from subprocess import check_call
check_call(['...PATH_TO_GRAPHVIZ/graphviz-2.38/release/bin/dot.exe','-Tpng','tree.dot','-o','tree.png'])
我也想在PyCharm中可视化决策树。有办法吗?
答案 0 :(得分:-1)
您可以安装bzixilu的名为dotplugin的插件, 当您打开点文件时,图形将自动显示在其旁边