0

I am trying to perform in python, cross-validation. My databaset have size 31x9. I want to split the matrix into matrixes with size 28 and 3 for the evaluation. Moreover in the test matrix I want to replace the last two columns equal to zero. My code is the following:

def printme(training):
    train_list = []
    test_list = []
    num_folds = 10
    subset_size = len(training)/num_folds
    for i in range(num_folds):
            testing_this_round = training[i*subset_size:][:subset_size]
            training_this_round = training[:i*subset_size] + training[(i+1)*subset_size:]
            train_list.append(training_this_round)
            new_test = testing_this_round

            new_test[0][8] = 0
            new_test[1][8] = 0
            new_test[2][8] = 0

            new_test[0][7] = 0
            new_test[1][7] = 0
            new_test[2][7] = 0

            test_list.append(new_test)

    return train_list, test_list

The crossvalidation seems to work fine, however when I am trying to make the last two columsn of test matrix equal to zero, I entounted problems. It seems that my code affect also the training matrix. How can I overcome this problem? In the returned matrices train_list and test_list in both of them the last two columns was changed to zero.

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152

1 Answers1

1

What you can do is importing copy and use its deepcopy function. Then you would just have to replace

testing_this_round = training[i*subset_size:][:subset_size]

by

testing_this_round = copy.deepcopy(training[i*subset_size:][:subset_size])

Actually your code was not affecting "also" the training list, since it was the same matrix, but with two different names. For more details, see e.g. python variables are pointers?

Community
  • 1
  • 1
keepAlive
  • 6,369
  • 5
  • 24
  • 39