简体   繁体   English

TypeError:列表索引必须是整数或切片,而不是电影分级数据的元组

[英]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.

相关问题 TypeError:列表索引必须是整数或切片,而不是元组? - TypeError: list indices must be integers or slices, not tuple? TypeError:列表索引必须是整数或切片,而不是元组 - TypeError: list indices must be integers or slices, not tuple 列表类型错误:列表索引必须是整数或切片,而不是元组 - List of lists TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是元组列表的元组 - TypeError: list indices must be integers or slices, not tuple for list of tuples 类型错误:列表索引必须是整数或切片,而不是在 python 中使用 sys 导入的元组 - TypeError: list indices must be integers or slices, not tuple with sys import in python 新编码器:TypeError:列表索引必须是整数或切片,而不是元组 - New coder: TypeError: list indices must be integers or slices, not tuple Python棋盘游戏-“类型错误:列表索引必须是整数或切片,而不是元组” - Python Board Game - "TypeError: list indices must be integers or slices, not tuple" Python 类型错误:列表索引必须是整数或切片,而不是元组 - Python TypeError: list indices must be integers or slices, not tuple 迭代字典会抛出 TypeError:列表索引必须是整数或切片,而不是元组 - Iterating on dictionary throws TypeError: list indices must be integers or slices, not tuple 如何修复“类型错误:列表索引必须是整数或切片,而不是元组” - How to fix "TypeError: list indices must be integers or slices, not tuple"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM