lightgbm特征选取
时间: 2023-09-01 10:11:33 浏览: 114
LightGBM有两种特征选择方法:基于树的重要性和基于排列重要性。
1. 基于树的重要性
LightGBM使用基于树的重要性来评估每个特征的重要性程度。该方法根据每个特征在树中出现的次数以及该特征分裂所带来的增益来计算特征的重要性。
可以通过以下步骤来获取特征的重要性程度:
```python
import lightgbm as lgb
# 训练模型
model = lgb.train(params, train_data, valid_sets=[train_data, val_data])
# 特征重要性
importance = model.feature_importance()
# 特征名称
feature_name = model.feature_name()
# 将特征重要性和名称组合成DataFrame并按重要性排序
df_importance = pd.DataFrame({'feature': feature_name, 'importance': importance}).sort_values('importance', ascending=False)
```
2. 基于排列重要性
基于排列重要性的特征选择方法可以更好地捕捉特征之间的相互作用,并且可以在特征数很大的情况下提供更准确的结果。它通过随机打乱单个特征的值并测量在新的数据集上性能的变化来评估特征的重要性。
可以通过以下步骤来获取特征的重要性程度:
```python
from sklearn.inspection import permutation_importance
# 计算特征排列重要性
result = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=42)
# 特征重要性
importance = result.importances_mean
# 特征名称
feature_name = X_test.columns
# 将特征重要性和名称组合成DataFrame并按重要性排序
df_importance = pd.DataFrame({'feature': feature_name, 'importance': importance}).sort_values('importance', ascending=False)
```
通过以上两种方法,我们可以获取到每个特征的重要性程度,从而进行特征筛选。
阅读全文
相关推荐


















