gpt4 book ai didi

python - 使用lightgbm的功能重要性

转载 作者:行者123 更新时间:2023-12-05 00:46:50 24 4
gpt4 key购买 nike

我正在尝试运行我的 lightgbm 进行功能选择,如下所示;

初始化

# Initialize an empty array to hold feature importances
feature_importances = np.zeros(features_sample.shape[1])

# Create the model with several hyperparameters
model = lgb.LGBMClassifier(objective='binary',
boosting_type = 'goss',
n_estimators = 10000, class_weight ='balanced')

然后我适合模型如下
# Fit the model twice to avoid overfitting
for i in range(2):

# Split into training and validation set
train_features, valid_features, train_y, valid_y = train_test_split(train_X, train_Y, test_size = 0.25, random_state = i)

# Train using early stopping
model.fit(train_features, train_y, early_stopping_rounds=100, eval_set = [(valid_features, valid_y)],
eval_metric = 'auc', verbose = 200)

# Record the feature importances
feature_importances += model.feature_importances_

但我收到以下错误
Training until validation scores don't improve for 100 rounds. 
Early stopping, best iteration is: [6] valid_0's auc: 0.88648
ValueError: operands could not be broadcast together with shapes (87,) (83,) (87,)

最佳答案

使用 lightgbm 模型时在 train 中获取特征重要性的示例。

import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

def plotImp(model, X , num = 20):
feature_imp = pd.DataFrame({'Value':model.feature_importance(),'Feature':X.columns})
plt.figure(figsize=(40, 20))
sns.set(font_scale = 5)
sns.barplot(x="Value", y="Feature", data=feature_imp.sort_values(by="Value",
ascending=False)[0:num])
plt.title('LightGBM Features (avg over folds)')
plt.tight_layout()
plt.savefig('lgbm_importances-01.png')
plt.show()

关于python - 使用lightgbm的功能重要性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53413701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com