# Decision tree classifier prediction changes from one run of the model to the next

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

• see here: stackoverflow.com/a/21394528/6020255
– oW_
Commented Aug 12, 2019 at 2:40
• @oW_, thank you that's it Commented Aug 12, 2019 at 3:38

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:

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

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!

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