使用布尔数组屏蔽2d数组

时间:2017-01-05 05:58:24

标签: python arrays knn

我正在尝试在python中为CIFAR-10训练集实现kfold验证,所以我试图使用布尔列表来掩盖训练集

X_Train_folds是一个形状为5X1000X3072的数组

selection = [True, True, True, True, True]
for k in k_choices:
    for i in xrange(0,num_folds):
        selection[i] = False

        classifier = KNearestNeighbor()
        classifier.train(X_train_folds[selection,:].reshape(1,X_train_folds.shape[0]*X_train_folds.shape[1])), 
                        Y_train_folds[selection,:].reshape(1,Y_train_folds.shape[0]*Y_train_folds.shape[1]))
        dists = classifier.compute_distances_no_loops(X_train_folds[i])
        y_pred = classifier.predict_labels(dists, k)
        num_correct = np.sum(y_pred == Y_train_folds[i])
        accuracy = float(num_correct) / (y_train.shape[0]/num_folds)
        k_to_accuracies[k] = accuracy




   TypeError: list indices must be integers, not tuple

编辑1:问题可以被理解为我试图得到像4行,除了循环中的第一行,如果数组是[1,2,3,4,5]首先我想要一个列表[ 2,3,4,5]然后[1,3,4,5]等等

1 个答案:

答案 0 :(得分:0)

如果您想连续删除列表中的每个第i个项目?

mask_this = [1,2,3,4,5]

[mask_this[:i]+mask_this[(i+1):] for i in range(len(mask_this))]  

Out[17]: [[2, 3, 4, 5], [1, 3, 4, 5], [1, 2, 4, 5], [1, 2, 3, 5], [1, 2, 3, 4]]