[英]TypeError: list indices must be integers or slices, not tuple for Movie Rating Data
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:列表索引必须是整数或切片,而不是元组”,并希望查看是否有人可以指出我的代码中的原因。
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
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. 而执行此操作的方法取决于
training_set
元素的结构。 For instance, if they are dictionaries, you can do this: 例如,如果它们是字典,则可以执行以下操作:
training_set_df = pandas.DataFrame(training_set)
And then use training_set_df
instead of training_set
. 然后使用
training_set_df
而不是training_set
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.