【发布时间】:2019-02-07 20:53:03
【问题描述】:
我在 scikit 中工作,我正在尝试调整我的 XGBoost。 我尝试使用嵌套交叉验证,使用管道重新调整训练折叠(以避免数据泄漏和过度拟合),并与 GridSearchCV 并行进行参数调整和 cross_val_score 以获得最后的 roc_auc 分数。
from imblearn.pipeline import Pipeline
from sklearn.model_selection import RepeatedKFold
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from xgboost import XGBClassifier
std_scaling = StandardScaler()
algo = XGBClassifier()
steps = [('std_scaling', StandardScaler()), ('algo', XGBClassifier())]
pipeline = Pipeline(steps)
parameters = {'algo__min_child_weight': [1, 2],
'algo__subsample': [0.6, 0.9],
'algo__max_depth': [4, 6],
'algo__gamma': [0.1, 0.2],
'algo__learning_rate': [0.05, 0.5, 0.3]}
cv1 = RepeatedKFold(n_splits=2, n_repeats = 5, random_state = 15)
clf_auc = GridSearchCV(pipeline, cv = cv1, param_grid = parameters, scoring = 'roc_auc', n_jobs=-1, return_train_score=False)
cv1 = RepeatedKFold(n_splits=2, n_repeats = 5, random_state = 15)
outer_clf_auc = cross_val_score(clf_auc, X_train, y_train, cv = cv1, scoring = 'roc_auc')
问题 1。
如何将cross_val_score 拟合到训练数据中?
问题2。
由于我在管道中包含了StandardScaler(),因此在cross_val_score 中包含X_train 是否有意义,或者我应该使用X_train 的标准化形式(即std_X_train)?
std_scaler = StandardScaler().fit(X_train)
std_X_train = std_scaler.transform(X_train)
std_X_test = std_scaler.transform(X_test)
【问题讨论】:
标签: scikit-learn nested pipeline cross-validation grid-search