2017-03-23 74 views
0

嘿,我是新来的Python,我试图用一个教程的内容,但我得到这个错误:NameError:名字“树”没有定义

NameError: name 'tree' is not defined.

目的显然是为节目根据要素的输入确定水果是苹果还是橘子。我在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]])) 
+1

你必须导入一些模块。它可能写在教程中。 – RafaelC

回答

10

添加到您的代码的顶部:

from sklearn import tree 

这是假设你正在学习的机器学习。

+1

@JacobElliott:但首先做点安装scikit学习:P –

+1

@AjaySingh希望他已经安装它。 – BoobyTrap

+1

我下载了anaconda,其中必须包含sklearn,因为它在我导入后工作。非常感谢@BoobyTrap,不相信我没有看到。 – JacobElliott

相关问题