我正在努力实现嵌套交叉验证。
我读过这个问题,但我正在尝试做一些不同的事情:Nested cross validation with StratifiedShuffleSplit in sklearn
我的数据:我有26个科目(每班13个)x 6670个功能。我使用了一种特征减少算法(您可能听说过Boruta)来减少数据的维数。问题现在开始:我将LOSO定义为外部分区模式。因此,对于26个cv折叠中的每一个,我使用24个对象来减少特征。这导致每个cv折叠中的不同数量的特征。现在,对于每个cv fold,我想使用相同的24个主题进行超参数优化(SVM与rbf内核)。
这就是我所做的:
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
但是,我收到以下错误消息:索引25超出了25的范围
任何帮助都会非常感激