【发布时间】:2018-07-04 10:54:42
【问题描述】:
我的数据框 urm 的形状为 (96438, 3)
user_id anime_id user_rating
0 1 20 7.808497
1 3 20 8.000000
2 5 20 6.000000
3 6 20 7.808497
4 10 20 7.808497
我正在尝试建立一个项目用户评分矩阵:
X = urm[["user_id", "anime_id"]].as_matrix()
y = urm["user_rating"].values
n_u = len(urm["user_id"].unique())
n_m = len(urm["anime_id"].unique())
R = np.zeros((n_u, n_m))
for idx, row in enumerate(X):
R[row[0]-1, row[1]-1] = y[idx]
如果代码成功,矩阵如下所示:(我用 0 填充 NaN)
在索引 user_id 中,anime_id 在列中,并为值评分(我从 pivot_table 中得到了这个矩阵)
在一些教程中它可以工作,但我有一个
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-278-0e06bd0f3133> in <module>()
15 R = np.zeros((n_u, n_m))
16 for idx, row in enumerate(X):
---> 17 R[row[0]-1, row[1]-1] = y[idx]
IndexError: index 5276 is out of bounds for axis 1 with size 5143
【问题讨论】:
-
请提供minimal reproducible example。在这种情况下,错误与您的数据不匹配。此外,向我们展示您对逻辑输出的期望。
标签: python pandas numpy indexoutofboundsexception