简体   繁体   English

使用布尔数组遮罩二维数组

[英]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 我正在尝试为CIFAR-10训练集在python中实现kfold验证,因此我试图使用布尔值列表掩盖训练集

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

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 编辑1:的问题可以理解为我试图获得类似4行,除了第i个在循环中一样,如果数组是[1,2,3,4,5]首先我想要[2,3]的列表,4,5],然后是[1,3,4,5],依此类推

If you want to just successively drop each i_th item in a list? 如果只想逐个删除列表中的第i_th个项目?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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