python - 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
解决方案
我认为有一个错误:
rating_c = userArtist[random_user[userArtist.artist].isnull().values & (userArtist.users != random_user)]
random_user,正如您所定义的,是一个字符串,并且您正在使用 Series 对其进行索引
random_user[userArtist.artist]
推荐阅读
- python - Python 神经网络中的形状问题
- nuxt.js - 为什么`nuxt generate`把commons.js文件放到dist/node_modules/
- javascript - 未解析编码查询参数的角度路由
- android - 我们如何将所有应用程序的流量路由到 Android 上的 SOCKS 服务器
- angular - 我可以获得角度材料表的标题单元格的索引吗?
- go - 使用 Go-bindata-assetfs 构建问题:找不到命令
- regexp-replace - Replacing GUID in Kusto
- python - 在 python3 上安装 socketio 模块似乎正在破坏 pip
- asp.net-mvc - 将 ASP.NET MVC 网站部署到 PLESK 时的连接字符串问题
- python-3.x - 使用 psycopg2 连接到 psql docker 容器但无法读取或写入