How Many Features Are Used in Random Forest When max_features is not an integer? In the RandomForestClassifier function in sklearn.ensemble, one of the parameters is max_features, which controls the size of the random subset of features that are considered at each node splitting. I would think this needs to be an integer, since of course the size of a subset needs to be an integer.
But one of the options here is &quot;log2&quot;, which sets max_features to log_2(n_features).
I was wondering how that works, considering log_2(n_features) will not be an integer unless n_features is a power of 2.
Thanks.
The argument for the number of features gets passed down to the BaseDecisionTree class. Looking at the code, you can see that the value that is used is calculated by first taking log with base two and then converting it to an integer (i.e. rounding it).
if self.max_features == &quot;auto&quot;:
    if is_classification:
        max_features = max(1, int(np.sqrt(self.n_features_)))
    else:
        max_features = self.n_features_
elif self.max_features == &quot;sqrt&quot;:
    max_features = max(1, int(np.sqrt(self.n_features_)))
elif self.max_features == &quot;log2&quot;:
    max_features = max(1, int(np.log2(self.n_features_)))
else:
    raise ValueError(&quot;Invalid value for max_features. &quot;
                     &quot;Allowed string values are 'auto', &quot;
                     &quot;'sqrt' or 'log2'.&quot;)