Neo4j 做推荐 (12)—— 协同过滤(基于邻域的推荐)

kNN——K近邻

现在我们有了一种基于偏好查找类似用户的方法,下一步是允许每个k最相似的用户投票选择应该推荐的项目。

主要有:                       

“与我的电影最相似的电影中有10个用户是谁?他们评价的电影有多高,我还没看过呢?”

使用Pearson相似度的kNN电影推荐

MATCH (u1:User{name:"Cynthia Freeman"})-[r:RATED]->(m:Movie)
WITH u1,avg(r.rating) AS u1_mean 

MATCH (u1)-[r1:RATED]-> (m:Movie)<-[r2:RATED]-(u2)
WITH u1, u1_mean, u2, COLLECT({r1:r1,r2:r2}) AS level WHERE size(level)> 10 

MATCH (u2)-[r:RATED]->(m:Movie)
WITH u1,u1_mean,u2,avg(r.rating)AS u2_mean,level

UNWIND level AS r 

WITH sum((r.r1.rating-u1_mean) * r.r2.rating-u2_mean))AS nom,
     sqrt(sum((r.r1.rating  -  u1_mean)^ 2)* sum((r.r2.rating  -  u2_mean)^ 2))AS denom,
     u1,u2 WHERE denom <> 0 

WITH u1,u2,nom / denom AS pearson 
ORDER BY pearson DESC LIMIT 10 

MATCH(u2)-[r:RATED]->(m:Movie)WHERE NOT EXISTS((u1)-[:RATED]->(m))

return m.title,SUM(pearson * r.rating) AS score
ORDER BY score DESC LIMIT 25

 

你可能感兴趣的:(Neo4j)