简体   繁体   中英

Masking 2d arrays using boolean array

I am trying to implement kfold validation in python for the CIFAR-10 training set so for that i am trying to mask the the training set using a boolean list

X_Train_folds is an array of shape 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

EDIT 1: The problem could be understood as i am trying to get like 4 rows except the ith one in the loop like if the array is [1,2,3,4,5] first i want a list of [2,3,4,5] then [1,3,4,5] and so on

If you want to just successively drop each i_th item in a list?

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM