# First steps with Python and scikit-learn

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.

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

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

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]]))