Is there a way to retrieve the list of feature names used for training of a classifier, once it has been trained with the fit
method? I would like to get this i
You don't need to know which features were selected for the training. Just make sure to give, during the prediction step, to the fitted classifier the same features you used during the learning phase.
The Random Forest Classifier will only use the features on which it makes its splits. Those will be the same as those learnt during the first phase. Others won't be considered.
If the shape of your test data is not the same as the training data it will throw an error, even if the test data contains all the features used for the splits of you decision trees.
What's more, since Random Forests make random selection of features for your decision trees (called estimators
in sklearn
) all the features are likely to be used at least once.
However, if you want to know the features used, you can just call the attributes n_features_
and feature_importances_
on your classifier once fitted.
You can look here to see how you can retrieve the names of the most important features you used.