直接上代码:
#bst为训练得到的模型
sum_fscore=0#fscore总和
sum_choose_fscore=0#前n个的fscore总和
a=bst.get_fscore()
num_features=n#需要前n个最重要特征
feature_n=[]#特征的值
column_filte=[]#特征名称
for i in a:
sum_fscore=sum_fscore+a[i]
feature_importance= sorted(a.items(), key=lambda d:d[1], reverse = True)
for i in range(num_features):
sum_choose_fscore=sum_choose_fscore+feature_importance[i][1]
feature_n.append(feature_importance[i][1])
column_filte.append(feature_importance[i][0])
print(sum_choose_fscore,sum_choose_fscore/sum_fscore)#总和,占总体比例,前n个最重要的特征
plt.bar(range(num_features),feature_n)#绘制前n个最重要特征
plt.show()
结果如下:
?
|