我正在努力实现协同过滤(使用Movielens 20m数据集)。
评级数据看起来像这样:
| userId | movieId | rating | timestamp |
评分在1-5之间(如果用户没有评价电影,它没有出现在表格中)。
以下是代码的一部分:
ratings = spark.read.option("inferSchema","true").option("header","true").csv("ratings.csv")
ratings.createOrReplaceTempView("ratings")
ratings.createOrReplaceTempView("ratings")
i_ratings = spark.sql("select distinct userId, case when movieId == 1 then rating else 0 end as rating from ratings order by userId asc ")
SQL查询意味着返回movieId == 1所有来自用户的评级,0表示未评级的用户。
我得到以下内容:dataframe
正如你所看到的,如果一个用户没有根据需要对电影进行评级,那么对于对电影评分的用户我会得到两行,一行是实际评级,另一行是评分= 0。
检查ratings.csv数据集,没有重复项,也就是说,每个用户最多为每部电影评分一次。
不知道我在这里失踪了什么。
尝试以下sql:
i_ratings = spark.sql("""
select
distinct userId,
case when rating is not null then rating else 0 end as rating
from ratings
where movieId = 1
order by userId asc
""")
不确定这是否是您想要的,但您的屏幕截图仅显示两列。即时猜测你想要以下内容:对于一个movieid,如果用户没有提供评级,那么把0评为其他评分。如果是这种情况,您应该使用where子句过滤moveId。