2017-03-23 76 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

相關問題