I tried looking in other articles on how to solve this issue but I am not getting anywhere for some reason. I get the error "TypeError: list indices must be integers or slices, not tuple" and wanted to see if anybody can point out the reason in my code.
我试着在其他文章中查找如何解决这个问题,但由于某些原因我没有得到任何结论。我得到错误“TypeError:list indices必须是整数或切片,而不是元组”,并想看看是否有人可以在我的代码中指出原因。
Thanks in advance!
提前致谢!
nb_users = int(max(max(training_set[:,0]), max(test_set[:,0])))
nb_movies = int(max(max(training_set[:,1]), max(test_set[:,1])))
def convert(data):
new_data = [] expects that. 1 list per user
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)
TypeError: Traceback (most recent call last)
<ipython-input-35-d2825f049d11> in <module>()
3
4 #take max user id/movie id for total numbers for test and train set.
----> 5 nb_users = int(max(max(training_set[:,0]), max(test_set[:,0])))
6 nb_movies = int(max(max(training_set[:,1]), max(test_set[:,1])))
7
TypeError: list indices must be integers or slices, not tuple
1 个解决方案
#1
0
You seem to use training_set
as a Pandas dataframe, but it is actually a list.
您似乎将training_set用作Pandas数据帧,但它实际上是一个列表。
You should first convert it to a dataframe; and the way to do this depends on the structure of training_set
's elements. For instance, if they are dictionaries, you can do this:
您应该首先将其转换为数据帧;并且这样做的方式取决于training_set元素的结构。例如,如果它们是字典,您可以这样做:
training_set_df = pandas.DataFrame(training_set)
And then use training_set_df
instead of training_set
.
然后使用training_set_df而不是training_set。
#1
0
You seem to use training_set
as a Pandas dataframe, but it is actually a list.
您似乎将training_set用作Pandas数据帧,但它实际上是一个列表。
You should first convert it to a dataframe; and the way to do this depends on the structure of training_set
's elements. For instance, if they are dictionaries, you can do this:
您应该首先将其转换为数据帧;并且这样做的方式取决于training_set元素的结构。例如,如果它们是字典,您可以这样做:
training_set_df = pandas.DataFrame(training_set)
And then use training_set_df
instead of training_set
.
然后使用training_set_df而不是training_set。