我无法手动匹配LGBM的交叉验证分数。
以下是一个MCVE示例:
from sklearn.datasets import load_breast_cancer
import pandas as pd
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import roc_auc_score
import lightgbm as lgb
import numpy as np
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
folds = KFold(5, random_state=42)
params = {'random_state': 42}
results = lgb.cv(params, lgb.Dataset(X_train, y_train), folds=folds, num_boost_round=1000, early_stopping_rounds=100, metrics=['auc'])
print('LGBM\'s cv score: ', results['auc-mean'][-1])
clf = lgb.LGBMClassifier(**params, n_estimators=len(results['auc-mean']))
val_scores = []
for train_idx, val_idx in folds.split(X_train):
clf.fit(X_train.iloc[train_idx], y_train.iloc[train_idx])
val_scores.append(roc_auc_score(y_train.iloc[val_idx], clf.predict_proba(X_train.iloc[val_idx])[:,1]))
print('Manual score: ', np.mean(np.array(val_scores)))
我原本希望两个CV分数是相同的——我设置了随机种子,并且做了完全相同的事情。但它们却不同。
以下是我得到的输出:
"最初的回答"
LGBM's cv score: 0.9851513530737058
Manual score: 0.9903622177441328
为什么我使用LGMB的
cv
模块时有问题?最初的回答。
num_boost_round
和early_stopping_rounds
参数?我看到你在__init__
方法或调用fit
时都没有显式传递它们。 - devforfunum_boost_round
和n_estimators
是一样的。如果我明确地设置了估计器数量,就不需要提前停止。 - EuRBamarth