sklearn 中 Ridge 的自定义交叉验证

问题描述 投票:0回答:0

我编写了以下算法来实现 Ridge 回归并通过交叉验证估计其参数。特别是,我想实现以下目标:

  1. 出于交叉验证的目的,将训练集分为 10 折。第一次,模型在 fold 1 上估计并在 fold 2 上验证;第二次估计第 1-2 折并在第 3 折上验证,...,第 9 次估计第 1-9 折并在第 10 折上验证。
  2. 对于上面的 9 个估计中的每一个,我希望使用训练中使用的折叠的均值和方差对训练特征进行 z 评分,并对验证特征进行 z 评分。

我在执行第 2 点的过程中做错了什么,但我不知道是什么。我可以对下面的实施发表意见吗?

# Create two lists of the indexes of the train and test sets as per point 1

n_splits=10
kf = KFold(n_splits=n_splits, shuffle=False)

folds = [idx for _, idx in kf.split(df_train)]
indexes_train = [folds[0]]
indexes_test = [folds[1]]

for i in range(1,n_splits-1):

   indexes_train.append(np.concatenate((np.array(indexes_train[i-1]), folds[i])))
   indexes_test.append(folds[i+1])
        
# Tune the model as per point 2

pipe = Pipeline(steps = [('scaler', StandardScaler()), ('model', Ridge(fit_intercept=True))])
alpha_tune = {'model__alpha': self.alpha_values} 
cross_validation = [i for i in zip(indexes_train, indexes_test)]
model = GridSearchCV(estimator=pipe, param_grid=alpha_tune, cv=cross_validation, scoring='neg_mean_squared_error', n_jobs=-1).fit(features_train, labels_train)

best_alpha = model.best_params_['model__alpha']
scikit-learn pipeline cross-validation
© www.soinside.com 2019 - 2024. All rights reserved.