【LightGBM】feature_importance获取特征重要性

使用LightGBM.feature_importance()函数给训练完毕的LightGBM模型的各特征进行重要性排序。

feature_importance             = pd.DataFrame()
feature_importance['fea_name'] = train_features
feature_importance['fea_imp']  = clf.feature_importance()
feature_importance             = feature_importance.sort_values('fea_imp',ascending = False)
 
plt.figure(figsize=[20,10],dpi=100)
ax = sns.barplot(x = feature_importance['fea_name'], y = feature_importance['fea_imp'])
ax.set_xticklabels(labels = ['file_id_api_nunique','file_id_api_count','file_id_tid_max','file_id_tid_mean','file_id_tid_min','file_id_tid_std','file_id_index_mean','file_id_tid_nunique','file_id_index_nunique','file_id_index_std','file_id_index_max','file_id_tid_count','file_id_index_count','file_id_index_min'],
                                    rotation = 45,fontsize = 15)
ax.set_yticklabels(labels = [0,2000,4000,6000,8000,10000,12000,14000,16000],fontsize = 15)
plt.xlabel('fea_name',fontsize=18)
plt.ylabel('fea_imp',fontsize=18)
# plt.tight_layout()
plt.savefig('D:/A_graduation_project/pictures/2_baseline1/特征重要性')

官方文档

feature_importance(importance_type='split', iteration=-1)
Get feature importances.

Parameters:
importance_type (string__, optional (default="split")) – How the importance is calculated. If “split”, result contains numbers of times the feature is used in a model. If “gain”, result contains total gains of splits which use the feature.
iteration (int or None, optional (default=None)) –  Limit number of iterations in the feature importance calculation. If None, if the best iteration exists,  it is used; otherwise, all trees are used. If <= 0, all trees are used(no limits).
Returns:
result – Array with feature importances.
Return type:
numpy array

————————————————
来源:https://blog.csdn.net/qq_41904729/article/details/117928981

  • 0
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LightGBM提供两种特征重要性计算方法:基于split的重要性和基于gain的重要性。 基于split的重要性计算方法:对于每个特征,根据该特征在树中出现的次数来计算其重要性。出现次数越多,重要性越高。 示例代码: ```python import lightgbm as lgb from lightgbm import Dataset # 加载数据集 data = Dataset('train_data.txt') # 训练模型 params = {'boosting_type': 'gbdt', 'objective': 'regression', 'metric': 'rmse'} model = lgb.train(params, data) # 获取特征重要性 importance_type = 'split' feature_importance = model.feature_importance(importance_type=importance_type) feature_names = model.feature_name() # 输出特征重要性 for feature_name, importance in zip(feature_names, feature_importance): print(feature_name, importance) ``` 基于gain的重要性计算方法:对于每个特征,根据该特征在树中的平均增益来计算其重要性。平均增益越大,重要性越高。 示例代码: ```python import lightgbm as lgb from lightgbm import Dataset # 加载数据集 data = Dataset('train_data.txt') # 训练模型 params = {'boosting_type': 'gbdt', 'objective': 'regression', 'metric': 'rmse'} model = lgb.train(params, data) # 获取特征重要性 importance_type = 'gain' feature_importance = model.feature_importance(importance_type=importance_type) feature_names = model.feature_name() # 输出特征重要性 for feature_name, importance in zip(feature_names, feature_importance): print(feature_name, importance) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值