我需要对一些数据进行估算并对其进行缩放,然后使用交叉验证来搜索我的模型(随机森林和逻辑回归分类器)的最佳超级参数集。另外,我想将整个事物包装成一个管道。
现在我使用这段代码:
param_grid = [{'solver':['liblinear',],
'Cs':[5, 10], 'penalty':['l1','l2'],
'tol':[0.1, 0.001, 0.0001],
'max_iter':[50,100,200]},
{'solver':['newton-cg', 'lbfgs', 'sag'],
'Cs':[5, 10,], 'penalty':['l2'],
'tol':[0.1, 0.001, 0.0001],
'max_iter':[50,100,200]}
]
clf = LogisticRegressionCV()
grid = GridSearchCV( estimator=clf, param_grid = param_grid)
pipe = Pipeline([('impute', Imputer(strategy='mean', axis=1)),
('scale', StandardScaler()),
('grid', grid_search)])
cv = StratifiedKFold(Ytrain, n_folds = 5, shuffle=True, random_state=2016)
score = cross_val_score(pipe, Xtrain, Ytrain, cv=cv)
print("Mean validation score: {0:.3f} (std: {1:.5f})".format(np.mean(score),np.std(score)))
然而,我感觉这种方式数据缩放和插补是全局发生的,这是不正确的。如何首先应用kfolding,然后分别对每个部分进行插补和缩放,然后执行模型拟合?