【问题标题】:Why is Pydot not installed?为什么没有安装 Pydot?
【发布时间】:2019-11-28 00:22:20
【问题描述】:

我正在浏览 Google 的机器学习视频。要完成其中一个模块,我必须 pip install pydot,它在命令提示符下成功完成。我回到我的代码,我不断收到一个错误-“名称'pydot'未定义”,但我多次确保我安装了它。我什至关闭了 jupyter 并重新打开它,我得到了同样的错误。任何帮助将不胜感激。

import numpy as np
import pydot
from sklearn.datasets import load_iris

iris = load_iris()
test_idx = [0, 50, 100]

train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx, axis=0)

test_target = iris.target[test_idx]
test_data = iris.data[test_idx]

clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)

print(test_target)
print(clf.predict(test_data))

from sklearn.externals.six import StringIO

dot_data = StringIO()
tree.export_graphviz(clf,
                out_file=dot_data,
                feature_names=iris.feature_names,
                class_names=iris.target_names,
                filled=True, rounded=True,
                impurity=False)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

print(test_data[0], test_target[0])

【问题讨论】:

  • 你用来运行脚本的python应该是你用来安装pydot的python/pip env。
  • 此代码不可能出现该错误。请将问题编辑为您正在使用的真实代码。还请包括完整的错误回溯消息。
  • 我发现 pydotplus 是我应该使用的模块,但我仍然遇到同样的错误。
  • 您可能正在为您的项目使用虚拟环境,但全局安装了 pydot,或者您可能有多个 python 版本并且您在 3.6 上安装了 pydot,但您正在 3.7 上运行您的项目但是如果您要去通过教程很可能他们让您创建了一个虚拟环境,但您并不完全理解虚拟环境的概念。发生在每个人身上

标签: python machine-learning pydot


【解决方案1】:

尝试在 IDLE 中运行您的代码,这可能是您的 IDE 无法识别该包。如果可行,您必须在项目设置中导入包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2016-04-03
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多