首页 > 解决方案 > 来自 sklearn 的 Graphviz 找不到我的文件?

问题描述

所以我正在尝试使用 sklearn 在 python 中构建决策树。

from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import graphviz

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 = DecisionTreeClassifier(random_state=0, max_depth=4)
tree.fit(X_train, y_train)

export_graphviz(tree, out_file=r"C:\Users\obaro\OneDrive\Documents\tree.dot", class_names=["malignant", "benign"],
               feature_names=cancer.feature_names, impurity=False, filled=True)

with open(r"C:\Users\obaro\OneDrive\Documents\tree.dot") as f:
    dot_graph = f.read()
    display(graphviz.Source(dot_graph))

但是,当我尝试在 Jupyter 中运行此代码时,我收到 FileNotFound 错误和 ExecutableNotFound 错误。起初,我尝试使用相对路径,但没有成功,所以我尝试使用绝对路径。该文件已创建并位于我当前的主目录中,因此我不确定这里发生了什么。任何帮助将不胜感激,谢谢。

标签: pythonscikit-learn

解决方案


推荐阅读