首页 > 解决方案 > 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]

标签: pandasmachine-learningscipysparse-matrix

解决方案


推荐阅读