RandomForest Feature Importance



Hi AV readers,

I am trying to solve classification problem using RF, and each time I run RandomForestClassifier(scikit) on my training data, feature importance shows different order of important features.How can I make sure it gives me same top 5 features every time I run the model ? Please help. Thanks!.

model_rc = RandomForestClassifier(n_estimators=10,max_depth=None,min_samples_split=2,random_state=0)
rc_fit=model_rc.fit(X_train, y_train.values.ravel())


Set the seed value using random.seed(). When you want to get the same result, execute the code with the same number.


Random Forest uses random number generator each to which you run your model. Which is causing it to use different sample and different features each time and giving you different output. There are two to handle this thing.

  1. Set same value other than 0 in random_state parameter in RandomForestClassifier method.
  2. As mentioned by gaurav use random.seed(some_number).
    This will cause random number generator to generate same value each time your model runs.