The short answer is that you did not pass random_state
fromto RandomForestClassifier
does not work as you think it does. Not sure if this is a bug or not.
This answers part ofTo clarify: if the questionalgorithm computes the score for feature A and then computes the score for feature B and it gets score N. Or if it computes first the score for feature B and then for feature A and it gets the same score N, but does not answeryou can see how each decision tree will be different, and have different scores during test, even if the question fullytrain test is the same (100% if max_depth=None of course).
I (You can hear you thinking:confirm this.)
Even if each DecisionTreeClassifier
has some randomness to it, I have used RandomForestClassifier(..., random_state=1)
, therefore all trees should be identical, right?
(Actually, re-reading During my exploration of your question, I see that you have not done thisproduced the following code with my own implementation of a random forest. But suppose that you didSince it took me some time, I figured I might as well paste it here. :)
I don't know why Seriously, but RandomForestClassifier does not pass along random_state
to each DecisionTreeClassifier
it can be useful. You can clearly see that this is the problem by implementing your own random forest, and tryingtry to enable and disable random_state
from my implementation to each DecisionTreeClassifier
.
Here,see what I have done that work for you:mean.
It produces different scores using the sklearn implementation of random forest, but the same scores using my implementation. HOWEVER, if you disable random_state=1
from each tree that I build, you can see that scores become different.
Summary: Clearly, RandomForestClassifier
is not passing along the random_state
you defined to each DecisionTreeClassifier
. While each two random forest with the same hyperparameters are identical, the same is not true to the trees within each random forest. (This could be a bug; I don't know.) But since eachEach DecisionTreeClassifier
is stochastic, data such as yours, which is small and comes from the same distribution, are bound to produce slightly different trees, even if the random forest itself is deterministic. You can fix this by passing the same seed to each DecisionTreeClassifier
which you can do using random_state=something
. RandomForestClassifier
also has a random_state
parameter which it passes along each DecisionTreeClassifier
.
EDIT: a previous version of this answer said that RandomForestClassifier
was not passing along random_state
to each DecisionTreeClassifier
. This was wrong, and was removed.