轉自:https://blog.csdn.net/m0_37477175/article/details/80567010
資料參考:
1. Evaluate Feature Importance using Tree-based Model
2. lgbm.fi.plot: LightGBM Feature Importance Plotting
3. lightgbm官方文檔
前言
基於樹的模型可以用來評估特征的重要性。 在本博客中,我將使用LightGBM中的GBDT模型來評估特性重要性的步驟。 LightGBM是由微軟發布的高精度和高速度梯度增強框架(一些測試表明LightGBM可以產生與XGBoost一樣的准確預測,但速度可以提高25倍)。
首先,我們導入所需的軟件包:用於數據預處理的pandas,用於GBDT模型的LightGBM以及用於構建功能重要性條形圖的matplotlib。
import pandas as pd
import matplotlib.pylab as plt
import lightgbm as lgb
1
2
3
然后,我們需要加載和預處理訓練數據。 在這個例子中,我們使用預測性維護數據集。
# read data
train = pd.read_csv('E:\Data\predicitivemaintance_processed.csv')
# drop the columns that are not used for the model
train = train.drop(['Date', 'FailureDate'],axis=1)
# set the target column
target = 'FailNextWeek'
# One-hot encoding
feature_categorical = ['Model']
train = pd.get_dummies(train, columns=feature_categorical)
1
2
3
4
5
6
7
8
9
10
11
12
接下來,我們用訓練數據訓練GBDT模型:
lgb_params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'num_leaves': 30,
'num_round': 360,
'max_depth':8,
'learning_rate': 0.01,
'feature_fraction': 0.5,
'bagging_fraction': 0.8,
'bagging_freq': 12
}
lgb_train = lgb.Dataset(train.drop(target, 1), train[target])
model = lgb.train(lgb_params, lgb_train)
1
2
3
4
5
6
7
8
9
10
11
12
13
模型訓練完成后,我們可以調用訓練模型的plot_importance函數來獲取特征的重要性。
plt.figure(figsize=(12,6))
lgb.plot_importance(model, max_num_features=30)
plt.title("Featurertances")
plt.show()
保存feature importance
booster = model.booster_
importance = booster.feature_importance(importance_type='split')
feature_name = booster.feature_name()
# for (feature_name,importance) in zip(feature_name,importance):
# print (feature_name,importance)
feature_importance = pd.DataFrame({'feature_name':feature_name,'importance':importance} )
feature_importance.to_csv('feature_importance.csv',index=False)
完美~
---------------------
作者:chestnut--
來源:CSDN
原文:https://blog.csdn.net/m0_37477175/article/details/80567010
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!