'Python error TypeError: list indices must be integers or slices, not tuple

def convert(data):
new_data = []
for id_users in range(1, nb_users + 1):  
    id_movies = data[:,1][data[:,0] == id_users]
    id_ratings = data[:,2][data[:,0]== id_users]
    Ratings = np.zeros(nb_movies)
    Ratings[id_movies - 1] = id_ratings
    new_data.append(list(Ratings))
return new_data


training_set = convert(training_set)
test_set = convert(test_set)

want apply this function to the training_set as well as the test_set, and to do this; we will our training_set followed by using the convert function on it. Inside the convert function, we will add the training_set that is the old version of the training_set, which will then become the new version, i.e., an array with the users in lines and the movies in the columns.

To do this, we will make two new variables, nb_users, which is going to be the total number of users and nb_movies that is going to be the total number of movies.

the new_data is a list of lists, so we need to initialize it as a list. After this, we will make a loop because we want to create a list for each user, the list of all the ratings of the movies by the user, and therefore, we need a for loop that will get the ratings for each use

error

error2



Solution 1:[1]

id_movies=data[:,1]data[:,0]==id_users]

write it as:

id_movies=data[:,1]data[:,0]==id_users

after removing the end bracket which is making the compiler to think it as a tuple instead of integer.

Apply same to id_ratings as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FlyingTeller