NameError:名称“树”未定义

新手上路,请多包涵

嘿,我是 Python 的新手,我正在尝试按照教程进行操作,但出现此错误:

NameError:未定义名称“树”。

目标显然是让程序根据输入的特征来判断水果是苹果还是橙子。我在 Win 10 上使用 Python 3.6 和 spyder 编辑器。我确信这很简单,感谢您的帮助!

 # -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

# features = [[140, "smooth"], [130, "smooth"], [150, "bumpy"], [170, "bumpy"]]
# labels = ["apple", "apple", "orange", "orange"]
features = [[140, 1], [130, 1], [150, 0], [170, 0]]
labels = [0, 0, 1, 1]
# We build a "Decision Tree" yes/no -> yes/no
# clf means classifier
clf = tree.DecisionTreeClassifier()
# Think of "fit" as "find patters in data"
clf = clf.fit(features, labels)
print (clf.predict([[160, 0]]))

原文由 JacobElliott 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 582
1 个回答

将此添加到代码的顶部:

 from sklearn import tree

这是假设您正在研究机器学习。

原文由 BoobyTrap 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题