importances = forest.feature_importances_ indices = np.argsort(importances)[::-1] # np.argsort(importances) # np.argsort(importances)[::-1] for f in range(X_train.shape[1]): print("%2d) % - *s %f" % (f + 1, 30, feat_labels[f], importances[indices[f]])) plt.title("Feature Importances") plt.bar(range(X_train.shape[1]), importances[indices], color="lightblue", align="center") plt.xticks(range(X_train.shape[1]), feat_labels, rotation=90) plt.xlim([-1, X_train.shape[1]]) plt.tight_layout() plt.show() from sklearn.feature_selection import SelectFromModel X_selected = forest.SelectFromModel(X_train, threshold=0.15) X_selected = SelectFromModel(forest, threshold=0.15) sfm.fit(X, y) n_features = sfm.transform(X).shape[1] X_selected = forest.transform(X_train, threshold=0.15) X_selected.shape