相关疑难解决方法(0)

scikit-learn GridSearchCV多次重复

我正在尝试为SVR模型获取最佳参数集.我想使用GridSearchCV超过不同的值C.但是,从之前的测试中我发现,分成训练/测试集高可影响整体表现(在这种情况下为r2).为了解决这个问题,我想实现重复的5倍交叉验证(10 x 5CV).是否有内置的方式来执行它GridSearchCV

快速解决方案:

遵循sci-kit 官方文档中提出的想法,快速解决方案代表:

NUM_TRIALS = 10
scores = []
for i in range(NUM_TRIALS):
     cv = KFold(n_splits=5, shuffle=True, random_state=i)
     clf = GridSearchCV(estimator=svr, param_grid=p_grid, cv=cv)
     scores.append(clf.best_score_)
print "Average Score: {0} STD: {1}".format(numpy.mean(scores), numpy.std(scores))
Run Code Online (Sandbox Code Playgroud)

python scikit-learn cross-validation grid-search

9
推荐指数
2
解决办法
7478
查看次数

配合使用带有管道和GridSearch的cross_val_score嵌套的交叉验证

我正在使用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, …
Run Code Online (Sandbox Code Playgroud)

pipeline nested scikit-learn cross-validation grid-search

1
推荐指数
1
解决办法
1105
查看次数