1
$\begingroup$

I'm running a very basic gender ['male', 'female'] classifier using the sklearn DecisionTreeClassifier based on [height, weight, shoe size] in a Jupyter notebook.

The prediction changes from male to female for the same input as I keep running the model.

I don't understand how that's possible. Shouldn't the build of the model be completely deterministic and therefore output the same prediction each time for a specific input?

Here's my code:

X = [[190, 90, 43], [165, 65, 38], [170, 70, 39], [160, 56, 36], [190, 88, 45], 
[164, 63, 37]]
Y = ['male', 'female', 'male', 'female', 'male', 'female']

clf = tree.DecisionTreeClassifier()
clf.fit(X, Y)

print(clf.predict([[200, 70, 37]]))
$\endgroup$
2

1 Answer 1

2
$\begingroup$

While this is a duplicate and the suggested link answers your question, for learning purposes I would like to suggest that you plot your DecisionTree every time you have a new run to see by yourself what happens behind the scenes:

from sklearn.tree import DecisionTreeClassifier
from sklearn.externals.six import StringIO  

from IPython.display import Image  
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()

X = [[190, 90, 43], [165, 65, 38], [170, 70, 39], [160, 56, 36], [190, 88, 45], [164, 63, 37]]
Y = ['male', 'female', 'male', 'female', 'male', 'female']

clf = DecisionTreeClassifier()
clf.fit(X, Y)

export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png())

Once you may get:

enter image description here

where splitting is done on X0 feature, where as next run you may get:

enter image description here

where splitting is done on X1 feature.

If you wanna reproduce your result at every run, you can use random_state = a_random_number and then you should expect to get the same result every time as the same tree has been constructed every time!

$\endgroup$
2
  • 1
    $\begingroup$ Very interesting, thank you for taking the time to write this up. $\endgroup$ Aug 12, 2019 at 9:42
  • 1
    $\begingroup$ Pleasure. Glad you find it helpful. $\endgroup$ Aug 12, 2019 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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