我想使用回调和 eval_set 等。 但我有一个问题:
from sklearn.multiclass import OneVsRestClassifier
import lightgbm
verbose = 100
params = {
"objective": "binary",
"n_estimators": 500,
"verbose": 0
}
fit_params = {
"eval_set": eval_dataset,
"callbacks": [CustomCallback(verbose)]
}
clf = OneVsRestClassifier(lightgbm.LGBMClassifier(**params))
clf.fit(X_train, y_train, **fit_params)
我如何将 fit_params 交给我的估算器?我明白了
----------------------------------------------------------------------
---> 13 clf.fit(X_train, y_train, **fit_params)
TypeError: OneVsRestClassifier.fit() got an unexpected keyword argument 'eval_set'
根据
scikit-learn
的 OneVsRestClassifier
文档(link),从 v1.4.0 开始,如果您启用了 **fit_params
所谓的“元数据”,则附加 fit()
仅会传递到估算器的 scikit-learn
方法路由”。
您的示例中缺少 2 个必需步骤:
sklearn.set_config(enable_metadata_routing=True)
scikit-learn
经过 eval_set
和 callbacks
,通过 .set_fit_request()
。(文档链接)
考虑这个使用 Python 3.11、
lightgbm==4.3.0
和 scikit-learn==1.4.1
的最小可重现示例。
import lightgbm as lgb
import sklearn
from sklearn.datasets import make_blobs
from sklearn.multiclass import OneVsRestClassifier
# enable metadata_routing
sklearn.set_config(enable_metadata_routing=True)
# create datasets
X, y = sklearn.datasets.make_blobs(
n_samples=10_000,
n_features=10,
centers=2
)
eval_dataset = lgb.Dataset(X, label=y)
eval_results = {}
# construct estimator
params = {
"objective": "binary",
"n_estimators": 10,
}
fit_params = {
"eval_set": (X, y),
"callbacks": [lgb.record_evaluation(eval_results)]
}
clf = OneVsRestClassifier(
lgb.LGBMClassifier(**params)
.set_fit_request(callbacks=True, eval_set=True)
)
# train
clf.fit(X, y, **fit_params)
# check eval results, to prove that the callback was used
print(eval_results)
# {'valid_0': OrderedDict([('binary_logloss', [0.598138869381609, 0.5203293282602738, 0.45544446427154844, 0.40059849184355334, 0.3537472248673818, 0.31338812592304066, 0.2783839141567028, 0.24785302530927006, 0.22109850424011224, 0.19756016345789282])])}