0

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

Ravin Kohli
  • 143
  • 10
  • The problem is Python doesn't have a true 2D array structure -- you could fake it using a dictionary instead of a list, or you could use something like numpy which has 2D arrays that use that syntax. – jackarms Jan 05 '17 at 06:02
  • i did not understand your second solution could you elaborate? and also you're talking about making the X_Train_folds as a dictionary? i think the dictionary one is a really good suggestion i will surely try it but i would like if you could elaborate on the second one as it seems more normal – Ravin Kohli Jan 05 '17 at 06:05
  • Maybe you could explain the problem you're trying to solve? It seems like you want to get 1 row, then 2 rows, etc. using the boolean array but I'm really not sure. – jackarms Jan 05 '17 at 06:11
  • no 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 – Ravin Kohli Jan 05 '17 at 06:12

1 Answers1

0

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]]
f5r5e5d
  • 3,656
  • 3
  • 14
  • 18