我有一个numpy用户项矩阵,每行对应一个用户,每列对应一个项。 我想在pandas DataFrame中转换矩阵,如下所示:
user item rating
0 1 1907 4.0
1 1 1028 5.0
2 1 608 4.0
3 1 2692 4.0
4 1 1193 5.0
我使用以下代码生成DataFrame:
predictions = pd.DataFrame(data=pred)
predictions = predictions.stack().reset_index(name='rating')
predictions.columns = ['user', 'item', 'rating']
我获得了这样的df:
user item rating
0 0 0 5.000000
1 0 1 0.000000
2 0 2 0.000000
3 0 3 0.000000
pandas中有没有办法将user和items列中的每个值映射到列表中存储的值?值为0的用户应使用用户列表中的第1个值进行映射,使用值为5的用户使用用户列表中的第6个元素进行映射,依此类推...... 我正在尝试使用:
predictions[["user"]].apply(lambda value: users[value])
但是我得到了一个我不理解的IndexError,因为我的用户列表大小为96
IndexError: ('index 96 is out of bounds for axis 1 with size 96', 'occurred at index user')
答案 0 :(得分:0)
我的错是在这段代码中:
while not session.should_stop():
predictions = session.run(decoder_op)
pred = np.vstack((pred, predictions))
刚刚替换为:
np.vstack((pred, predictions))
它的魅力就像:
predictions['user'] = predictions['user'].map(lambda value: users[value])
predictions['item'] = predictions['item'].map(lambda value: items[value])