pandas - AttributeError:“MultiIndex”对象没有属性“标签”
问题描述
目前正在研究电影推荐系统。试图将 userId 和 movieId 设置为索引,以使它们成为稀疏矩阵的 x 和 y 轴。但收到此错误:
> ---------------------------------------------------------------------------
> AttributeError Traceback (most recent call last)
<ipython-input-34-45d9a0074103> in <module>
11 ratings_df.set_index(['userId', 'movieId'], inplace=True)
12 ratings_matrix = sps.csr_matrix((ratings_df.rating,
---> 13 (ratings_df.index.labels[0], ratings_df.index.labels[1])))
14
15 print('shape ratings_matrix:', ratings_matrix.todense())
> AttributeError: 'MultiIndex' object has no attribute 'labels'
这是代码片段:
import scipy.sparse.linalg
from sklearn.metrics import mean_absolute_error
import scipy.sparse as sps
# set userId and movieId as indexes in order to make them as x and y axis of sparse matrix
ratings_df = ratings_df_raw.copy()
print('unique users:', len(set(ratings_df['userId'])))
print('unique movies:', len(set(ratings_df['movieId'])))
ratings_df.set_index(['userId', 'movieId'], inplace=True)
ratings_matrix = sps.csr_matrix((ratings_df.rating,
(ratings_df.index.labels[0], ratings_df.index.labels[1]))).todense()
print('shape ratings_matrix:', ratings_matrix.shape)
编辑:ratings_df_raw
userId movieId rating
0 0 30 2.5
1 6 30 3.0
2 30 30 4.0
3 31 30 4.0
4 35 30 3.0
... ... ... ...
99999 663 7103 2.5
100000 663 7359 3.5
100001 664 115 3.0
100002 664 3712 1.0
100003 667 4629 1.0
[100004 rows x 3 columns]
解决方案
推荐阅读
- javascript - 如何在我的 Chrome 应用中使用 Javascript 检测 Chromebook 是否处于开发者模式?
- visual-c++ - YAXPBD 和 YAXPEBD 有什么区别?
- apache - 主机上的 Apache 需要密码,但不接受用户凭据
- python - 是否可以使用 babeltrace Python API(或任何其他软件工具)来读取“实时”LTTng 跟踪?
- angular - 分页 Angular Firestore (FireBase)
- excel - Excel VBA 更改列上的时间/日期格式
- python - python中连接列表和元组的行为不一致
- angular - 如何禁用/启用下拉角4
- tcp - 为什么我们需要 NDS 服务器(不是顶级 DNS)即使我的域在二级
- javascript - 如何在登录 Firebase 时获取用户信息