排列表示:
importances = model.feature_importances_
indices = np.argsort(importances)[::-1]
feat_labels = X_train.columns
print("Feature ranking:")
# l1,l2,l3,l4 = [],[],[],[]
for f in range(X_train.shape[1]):
print("%d. feature no:%d feature name:%s (%f)" % (f + 1, indices[f], feat_labels[indices[f]], importances[indices[f]]))
print (">>>>>", importances)
画图:
feature_importance = model.feature_importances_
sorted_idx = np.argsort(feature_importance)
features_list = data.columns.values
plt.figure(figsize=(5,20))
plt.barh(range(len(sorted_idx)), feature_importance[sorted_idx], align='center')
plt.yticks(range(len(sorted_idx)), features_list[sorted_idx],)
plt.xlabel('Importance')
plt.title('Feature importances')
plt.draw()
plt.show()
筛选重要程度大于某个值的特征:
#将特征名称与对应的重要性数值做成dataframe
fea = pd.DataFrame()
fea['feature_name'] = feature_name
fea['value'] = feature_importance
fea.loc[fea['value'] >0 ]
#找出重要性为0的特征
fea_0 = fea.loc[fea['value'] == 0 ]['feature_name'].tolist()
fea_0