numpy:如何 select np 数组中的特定索引以进行 k 折交叉验证?

numpy: How can I select specific indexes in an np array for k-fold cross validation?

我有一个矩阵形式的训练数据集,尺寸为 5000 x 3027(CIFAR-10 数据集)。在 numpy 中使用 array_split,我将它分成 5 个不同的部分,我想 select 只是其中一个部分作为交叉验证折叠。但是,当我使用类似的东西时,我的问题就来了 XTrain[[Indexes]] 其中 indexes 是一个数组,如 [0,1,2,3],因为这样做会给我一个尺寸为 4 x 1000 x 3027 的 3D 张量,而不是矩阵。如何将“4 x 1000”折叠成 4000 行,以获得 4000 x 3027 的矩阵?

for fold in range(len(X_train_folds)):
    indexes = np.delete(np.arange(len(X_train_folds)), fold) 
    XTrain = X_train_folds[indexes]
    X_cv = X_train_folds[fold]
    yTrain = y_train_folds[indexes]
    y_cv = y_train_folds[fold]

    classifier.train(XTrain, yTrain)
    dists = classifier.compute_distances_no_loops(X_cv)
    y_test_pred = classifier.predict_labels(dists, k)

    num_correct = np.sum(y_test_pred == y_test)
    accuracy = float(num_correct/num_test)
    k_to_accuracy[k] = accuracy

我建议使用 scikit-learn package. It already comes with plenty of common machine learning tools, such as K-fold cross-validation generator:

>>> from sklearn.cross_validation import KFold
>>> X = # your data [samples x features]
>>> y = # gt labels
>>> kf = KFold(X.shape[0], n_folds=5)

然后,遍历 kf:

>>> for train_index, test_index in kf:
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
        # do something

上面的循环会执行n_folds次,每次都有不同的训练和测试指标。

也许你可以试试这个(numpy 的新手,所以如果我正在做某事 inefficient/wrong,很乐意得到纠正)

X_train_folds = np.array_split(X_train, num_folds)
y_train_folds = np.array_split(y_train, num_folds)
k_to_accuracies = {}

for k in k_choices:
    k_to_accuracies[k] = []
    for i in range(num_folds):
        training_data, test_data = np.concatenate(X_train_folds[:i] + X_train_folds[i+1:]), X_train_folds[i]
        training_labels, test_labels = np.concatenate(y_train_folds[:i] + y_train_folds[i+1:]), y_train_folds[i]
        classifier.train(training_data, training_labels)
        predicted_labels = classifier.predict(test_data, k)
        k_to_accuracies[k].append(np.sum(predicted_labels == test_labels)/len(test_labels))