gpt4 book ai didi

python-3.x - 使用LightGBM的网格搜索示例

转载 作者:行者123 更新时间:2023-12-04 18:56:51 25 4
gpt4 key购买 nike

我正在尝试使用lightgbm中的GridSearchCVsklearn.model_selection模型找到最佳参数。我还没有找到实际可行的解决方案。

我设法建立了部分工作的代码:

import numpy as np
import pandas as pd
import lightgbm as lgb
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold

np.random.seed(1)

train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
y = pd.read_csv('y.csv')
y = y.values.ravel()
print(train.shape, test.shape, y.shape)

categoricals = ['COL_A','COL_B']
indexes_of_categories = [train.columns.get_loc(col) for col in categoricals]

gkf = KFold(n_splits=5, shuffle=True, random_state=42).split(X=train, y=y)

param_grid = {
'num_leaves': [31, 127],
'reg_alpha': [0.1, 0.5],
'min_data_in_leaf': [30, 50, 100, 300, 400],
'lambda_l1': [0, 1, 1.5],
'lambda_l2': [0, 1]
}

lgb_estimator = lgb.LGBMClassifier(boosting_type='gbdt', objective='binary', num_boost_round=2000, learning_rate=0.01, metric='auc',categorical_feature=indexes_of_categories)

gsearch = GridSearchCV(estimator=lgb_estimator, param_grid=param_grid, cv=gkf)
lgb_model = gsearch.fit(X=train, y=y)

print(lgb_model.best_params_, lgb_model.best_score_)

这似乎正在工作,但是使用了 UserWarning:

categorical_feature keyword has been found in params and will be ignored. Please use categorical_feature argument of the Dataset constructor to pass this parameter.



我正在寻找一个可行的解决方案,或者是关于如何确保lightgbm接受上述代码中的分类参数的建议

最佳答案

如警告状态所示,categorical_feature不是LGBMModel参数之一。它与lgb.Dataset实例化相关,在sklearn API的情况下,直接在fit()方法see the doc中完成。因此,为了在GridSearchCV优化中传递这些参数,在sklearn v0.19.1的情况下,必须将其作为GridSearchCV.fit()方法的参数提供,或者在较旧的sklearn版本中作为fit_params实例化的附加GridSearchCV参数提供

关于python-3.x - 使用LightGBM的网格搜索示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686645/

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