带有 lightgbm 的 GridSearchCV 需要不使用 fit() 方法?
GridSearchCV with lightgbm requires fit() method not used?
我正在尝试在 LightGBM 估计器上使用 sklearn
执行 GridSearchCV
,但在构建搜索时 运行 遇到了问题。
我要构建的代码如下所示:
d_train = lgb.Dataset(X_train, label=y_train)
params = {}
params['learning_rate'] = 0.003
params['boosting_type'] = 'gbdt'
params['objective'] = 'binary'
params['metric'] = 'binary_logloss'
params['sub_feature'] = 0.5
params['num_leaves'] = 10
params['min_data'] = 50
params['max_depth'] = 10
clf = lgb.train(params, d_train, 100)
param_grid = {
'num_leaves': [10, 31, 127],
'boosting_type': ['gbdt', 'rf'],
'learning rate': [0.1, 0.001, 0.003]
}
gsearch = GridSearchCV(estimator=clf, param_grid=param_grid)
lgb_model = gsearch.fit(X=train, y=y)
但是我 运行 遇到以下错误:
TypeError: estimator should be an estimator implementing 'fit' method,
<lightgbm.basic.Booster object at 0x0000014C55CA2880> was passed
但是 LightGBM 是使用 train()
方法而不是 fit()
训练的,因此这个网格搜索不可用吗?
谢谢
您正在使用的 lgb
对象不支持 scikit-learn
API。这就是为什么您不能以这种方式使用它的原因。
但是,lightgbm
包提供 类 与 scikit-learn
API 兼容。根据您要完成的监督学习任务,分类或回归,使用 LGBMClassifier
or LGBMRegressor
。分类任务示例:
from lightgbm import LGBMClassifier
from sklearn.model_selection import GridSearchCV
clf = LGBMClassifier()
param_grid = {
'num_leaves': [10, 31, 127],
'boosting_type': ['gbdt', 'rf'],
'learning rate': [0.1, 0.001, 0.003]
}
gsearch = GridSearchCV(estimator=clf, param_grid=param_grid)
gsearch.fit(X_train, y_train)
我正在尝试在 LightGBM 估计器上使用 sklearn
执行 GridSearchCV
,但在构建搜索时 运行 遇到了问题。
我要构建的代码如下所示:
d_train = lgb.Dataset(X_train, label=y_train)
params = {}
params['learning_rate'] = 0.003
params['boosting_type'] = 'gbdt'
params['objective'] = 'binary'
params['metric'] = 'binary_logloss'
params['sub_feature'] = 0.5
params['num_leaves'] = 10
params['min_data'] = 50
params['max_depth'] = 10
clf = lgb.train(params, d_train, 100)
param_grid = {
'num_leaves': [10, 31, 127],
'boosting_type': ['gbdt', 'rf'],
'learning rate': [0.1, 0.001, 0.003]
}
gsearch = GridSearchCV(estimator=clf, param_grid=param_grid)
lgb_model = gsearch.fit(X=train, y=y)
但是我 运行 遇到以下错误:
TypeError: estimator should be an estimator implementing 'fit' method,
<lightgbm.basic.Booster object at 0x0000014C55CA2880> was passed
但是 LightGBM 是使用 train()
方法而不是 fit()
训练的,因此这个网格搜索不可用吗?
谢谢
您正在使用的 lgb
对象不支持 scikit-learn
API。这就是为什么您不能以这种方式使用它的原因。
但是,lightgbm
包提供 类 与 scikit-learn
API 兼容。根据您要完成的监督学习任务,分类或回归,使用 LGBMClassifier
or LGBMRegressor
。分类任务示例:
from lightgbm import LGBMClassifier
from sklearn.model_selection import GridSearchCV
clf = LGBMClassifier()
param_grid = {
'num_leaves': [10, 31, 127],
'boosting_type': ['gbdt', 'rf'],
'learning rate': [0.1, 0.001, 0.003]
}
gsearch = GridSearchCV(estimator=clf, param_grid=param_grid)
gsearch.fit(X_train, y_train)