graphvizの実行ファイルをインストール
https://graphviz.gitlab.io/_pages/Download/Download_windows.html
pythonにgraphvizをインストール
pip install graphviz
pythonからサンプルコード実行
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifiercancer = 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 = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)from sklearn.tree import export_graphviz
export_graphviz(tree, out_file=”tree.dot”, class_names=[“malignant”, “benign”],feature_names=cancer.feature_names, impurity=False, filled=True)import graphviz
with open(“tree.dot”) as f:
dot_graph = f.read()
display(graphviz.Source(dot_graph))