I've this code to print the importance of each variable on my model:

importances = trained_model.feature_importances_
std = np.std([trained_model.feature_importances_ for trained_model in trained_model.estimators_], axis=0)
indices = np.argsort(importances)[::-1]

# Print the feature ranking
print("Feature ranking:")

for f in range(training_features.shape[1]):
    print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))

I print a lot of variables with feature ranking as 0.0. Should I remove that variables? I can I do it using Python?

Like this:

df = df.drop('Col_A', 1) WHERE importances[indices[f]] = 0


  • $\begingroup$ Yes they can be removed if it's 0 like thresholding and then again we can retrain our .ideal to see the difference? $\endgroup$
    – Aditya
    Commented Mar 1, 2018 at 17:23

1 Answer 1


Assuming your features are df[X] and your target is df[y], I would just do the following:

keepfeatures = X[trained_model.feature_importances_ > 0]

df = df[keepfeatures]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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