3
$\begingroup$

I believe I have a simple if not trivial question. I have a background in statistics and I tend to use Stata and R quite a bit. I am interested in learning Python. I used it for a while now and recently came into contact with scikit-learn.

I am trying to reproduce a simple example.

from sklearn import tree
features = [[140, 1],[130, 1], [150, 0], [170, 0]]
labels = [0 , 0 , 1 , 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
print clf.predict([[150, 0]])

As you can see, the tiny script tries to predict - by the means of a decision tree - wether a object with the properties [150, 0] is likely a type 1 or 0.

I run the script and get following error:

File "clf_decision_tree.py", line 6
    print clf.predict([[150, 0]])
            ^
SyntaxError: invalid syntax

I realy don't get what is wrong... Can you help me out? Best /R

PS: I not sure if Cross Validated or Stackoverflow are better places to ask. Let me know. Thanks.

$\endgroup$
  • 1
    $\begingroup$ It's also unnecessary to assign the result from fit back onto clf, it is an inplace method which changes your clf object $\endgroup$ – Jan van der Vegt Aug 16 '16 at 16:55
  • 1
    $\begingroup$ what version of python are you using? $\endgroup$ – njzk2 Aug 16 '16 at 20:56
  • $\begingroup$ Very useful, it solved the problem. $\endgroup$ – Lavoisier Farias Mar 5 '17 at 23:59
10
$\begingroup$

In python 3 the print function must have parenthesis, so print(clf.predict([[150, 0]])) will work

$\endgroup$
1
$\begingroup$
from sklearn import tree

features = [[2200,1], [1500,1], [1800,1], [900,2], [1000,2]]

labels = ['SUV', 'SUV', 'SUV', 'hatchback', 'hatchback']

clf = tree.DecisionTreeClassifier()

clf = clf.fit(features, labels)

print(clf.predict([[1350, 1]]))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.