1

我正在努力实现嵌套交叉验证。

我读了这个问题,但我想做一些不同的事情:Nested cross validation with StratifiedShuffleSplit in sklearn

我的数据:我有 26 个科目(每班 13 个)x 6670 个特征。我使用了一种特征减少算法(您可能听说过 Boruta)来减少我的数据的维数。问题从现在开始:我将 LOSO 定义为外部分区模式。因此,对于 26 个 cv 折叠中的每一个,我使用 24 个主题来减少特征。这导致每个 cv 折叠中的特征数量不同。现在,对于每个 cv 折叠,我想使用相同的 24 个主题进行超参数优化(带有 rbf 内核的 SVM)。

这就是我所做的:

cv = list(LeaveOneout(len(y))) # in y I stored the labels
    
inner_train = [None] * len(y)

inner_test =  [None] * len(y)

ii = 0

while ii < len(y):
    cv = list(LeaveOneOut(len(y))) 
    a = cv[ii][0]
    a = a[:-1]
    inner_train[ii] = a

    b = cv[ii][0]
    b = np.array(b[((len(cv[0][0]))-1)])
    inner_test[ii]=b

    ii = ii + 1

custom_cv = zip(inner_train,inner_test) # inner cv


pipe_logistic = Pipeline([('scl', StandardScaler()),('clf', SVC(kernel="rbf"))])

parameters = [{'clf__C':  np.logspace(-2, 10, 13), 'clf__gamma':np.logspace(-9, 3, 13)}]



scores = [None] * (len(y)) 

ii = 0

while ii < len(scores):

    a = data[ii][0] # data for train
    b = data[ii][1] # data for test
    c = np.concatenate((a,b)) # shape: number of subjects * number of features
    d = cv[ii][0] # labels for train
    e = cv[ii][1] # label for test
    f = np.concatenate((d,e))
    
    grid_search = GridSearchCV(estimator=pipe_logistic, param_grid=parameters, verbose=1, scoring='accuracy', cv= zip(([custom_cv[ii][0]]), ([custom_cv[ii][1]])))

    scores[ii] = cross_validation.cross_val_score(grid_search, c, y[f], scoring='accuracy', cv = zip(([cv[ii][0]]), ([cv[ii][1]])))
    
    ii = ii + 1

但是,我收到以下错误消息:index 25 is out of bounds for size 25

任何帮助将非常感激

4

0 回答 0