电影评分用户ID索引

问题描述 投票:0回答:1

我正在尝试使用rating.csv构建电影推荐系统。我通过user_id和movie_id使用ivot()构建了一个评分矩阵。

但是,我需要构建一个字典,其中的键是用户ID,值是我构建的矩阵中匹配的行索引。

mydf = mydf.pivot(index='user_Id', columns='movie_Id', values='rating').fillna(0).astype(int)

mymatrix = mydf.as_matrix()

我希望这样:

users_index = {22: 0, 23: 1, 25: 2, 31: 3, 142: 4}
python numpy
1个回答
0
投票

只需这样做:

dict(zip(list(mydf.index), list(range(len(mydf.index)))))

{22: 0, 23: 1, 25: 2, 31: 3, 142: 4}

这里是完整代码

user_Id;movie_Id;rating
22;453;0
23;545;1
25;642;2
31;237;3
142;348;4

import pandas as pd
mydf = pd.read_clipboard(sep=';')
mydf = mydf.pivot(index='user_Id', columns='movie_Id', values='rating').fillna(0).astype(int)


dict(zip(list(mydf.index), list(range(len(mydf.index)))))

{22: 0, 23: 1, 25: 2, 31: 3, 142: 4}

© www.soinside.com 2019 - 2024. All rights reserved.