I am trying to do a 10-fold cross validation for a sentiment classifier. For this purpose, I have created a list of 10 lists, where each list corresponds to one of the folds and contains movie reviews.
I am trying to create a loop, where for every iteration I use 9 folds for training the classifier and one fold for testing it. However, I am facing difficulties with subsetting the list of lists to create two variables (one for the fold, and one for the 9 remaining folds), which I can pass through my train and test functions.
I created this example as a more-readable version of my code:
list1 = [{"ID":1, "sentiment":"positive", "content": "further lists within lists"}]
list2 = [{"ID":2, "sentiment":"positive", "content": "further lists within lists"}]
list3 = [{"ID":3, "sentiment":"positive", "content": "further lists within lists"}]
list4 = [{"ID":4, "sentiment":"positive", "content": "further lists within lists"}]
list5 = [{"ID":5, "sentiment":"positive", "content": "further lists within lists"}]
list_of_lists = [list1, list2, list3, list4, list5]
for list_ in list_of_lists:
remaining_lists = list_of_lists[~list_]
train_classifier(remaining_lists)
test_classifier(list_)
The error I get is "bad operand type for unary ~: 'list'". I have seen the answers to a related question at Index all *except* one item in python, but I could not implement the solutions suggested in a loop.