我正在努力实现协同过滤(使用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,但是对于对电影进行评级的用户,我将获得两行,一行具有实际评级,另一行评分为= 0。
检查了rating.csv数据集,没有重复项,也就是说,每个用户最多为每部电影评分一次。
不确定我在这里缺少什么。
答案 0 :(得分:0)
尝试以下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。