NameError:name' tree'没有定义

时间:2017-03-23 14:35:46

标签: python python-3.x scikit-learn spyder

嘿,我是Python的新手,我正在尝试按照教程进行操作但是我收到了这个错误:

  

NameError:name' tree'没有定义。

目标显然是程序根据特征输入确定水果是苹果还是橙子。我在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]]))

3 个答案:

答案 0 :(得分:12)

将其添加到代码顶部:

from sklearn import tree

这假设您正在学习机器学习。

答案 1 :(得分:0)

from sklearn.tree import DecisionTreeClassifier

答案 2 :(得分:-1)

我尝试了这个from sklearn import tree,但它在我的系统上不起作用。

然后我尝试了from sklearn.tree import DecisionTreeClassifier并开始工作。

这当然是在安装sklearn之后。