0
$\begingroup$

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 "log2", 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.

$\endgroup$
1
$\begingroup$

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 == "auto":
    if is_classification:
        max_features = max(1, int(np.sqrt(self.n_features_)))
    else:
        max_features = self.n_features_
elif self.max_features == "sqrt":
    max_features = max(1, int(np.sqrt(self.n_features_)))
elif self.max_features == "log2":
    max_features = max(1, int(np.log2(self.n_features_)))
else:
    raise ValueError("Invalid value for max_features. "
                     "Allowed string values are 'auto', "
                     "'sqrt' or 'log2'.")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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