首页 > 解决方案 > python - 为喜欢的用户返回前三名艺术家 - 数据类型错误

问题描述

我的代码打算为随机用户生成三个艺术家推荐。这是一个基于用户的协作过滤模型,但我遇到了一个错误,它读取TypeError: string indices must be integers, not Series指向我分配的行rating_c

我检查了数据类型,但无法检测到该变量中的哪个部分正在注册为系列。有没有人看到可能是系列?

下面是df头:

userArtist.head()

    users   artist             gender      plays    age
0   a       aesop rock         m           72       28.0
1   b       air                m           178      28.0
2   c       amon tobin         m           106      28.0
3   d       animal collective  m           203      28.0
4   e       annie              m           75       28.0

代码:

music_mat = userArtist.pivot_table(index='artist', columns='users', values='plays').fillna(0).apply(np.sign)
# user-item collaberative filtering of random user 
random_user = np.random.choice(userArtist.users)
# Check df for values of random user
random_user_rating = music_mat[random_user]
# Pairwise correlation of top five similar users 
user_corr = music_mat.corrwith(random_user_rating)[:5]

rating_c = userArtist[(userArtist.artist.isnull().values) & (userArtist.users != random_user)]
rating_c['similarity'] = rating_c['users'].map(user_corr.get)
rating_c['sim_rating'] = rating_c.similarity * rating_c.plays

recommendation = rating_c.groupby('artist').apply(lambda s: s.sim_rating.sum() / s.similarity.sum())
recommendation

标签: pythonpandascollaborative-filtering

解决方案


我认为有一个错误:

rating_c = userArtist[random_user[userArtist.artist].isnull().values & (userArtist.users != random_user)]

random_user,正如您所定义的,是一个字符串,并且您正在使用 Series 对其进行索引

random_user[userArtist.artist]

推荐阅读