cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Find the items with the most similar preferences to movie Titanic, according to cosine similarity

miklov
Node Link

Hi,
I've been trying to code the above r from the MOVIE Lens database and i dont really get how to do it. Can someone help? I just registered with the community  and i dont really understand how this works, i would appreciate any help.

I tried the following code

MATCH (p1:Movie)-[x:RATED]->(Movie{title: 'Titanic'})<-[x2:RATED]-(p2:Movie)

WHERE p2 <> p1

WITH p1, p2, collect(x.rating) AS p1Ratings, collect(x2.rating) AS p2Ratings

WHERE size(p1Ratings) > 10

RETURN p1.name AS from,

       p2.name AS to,

       gds.similarity.cosine(p1Ratings, p2Ratings) AS similarity

ORDER BY similarity DESC

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @miklov 😊

This query should help you, I did on the Movie database (there is no result for this movie):

 

MATCH (m:Movie {title: 'The Da Vinci Code'})
WITH m, [(m)<-[r:REVIEWED]-(:Person) | r.rating] AS ratings_m
CALL {
    WITH m, ratings_m
    MATCH (n:Movie)
    WHERE m <> n
    WITH ratings_m, n, [(n)<-[r:REVIEWED]-(:Person) | r.rating] AS ratings_n
    WHERE size(ratings_n) > 0 AND size(ratings_m) = size(ratings_n)
    RETURN n, gds.similarity.cosine(ratings_m, ratings_n) AS similarity
}
RETURN m.title AS m1, n.title AS m2, similarity

 

Be careful, the lists of values in the similarity function must have the same size.

Regards,
Cobra

View solution in original post

5 REPLIES 5

Cobra
Ninja
Ninja

Hello @miklov 😊

This query should help you, I did on the Movie database (there is no result for this movie):

 

MATCH (m:Movie {title: 'The Da Vinci Code'})
WITH m, [(m)<-[r:REVIEWED]-(:Person) | r.rating] AS ratings_m
CALL {
    WITH m, ratings_m
    MATCH (n:Movie)
    WHERE m <> n
    WITH ratings_m, n, [(n)<-[r:REVIEWED]-(:Person) | r.rating] AS ratings_n
    WHERE size(ratings_n) > 0 AND size(ratings_m) = size(ratings_n)
    RETURN n, gds.similarity.cosine(ratings_m, ratings_n) AS similarity
}
RETURN m.title AS m1, n.title AS m2, similarity

 

Be careful, the lists of values in the similarity function must have the same size.

Regards,
Cobra

Hello Cobra,

Thank you very very much for the quick reply, appreciated. So the outcome should be (no changes, no records)?  Also i have a few more questions on this database, would you be able to help?

Thank you very much,

Mikaella 

The movie The Da Vinci Code has no similar movies so there is no result but this query can be transposed to other databases. The Movie database is not the best one to test GDS algorithms.

Feel free to ask your questions.

Regards,
Cobra

Thank you very much. I have send you a private message 🙂

Hi miklov,

I know you've already gotten some help here, but there is a semantic error in your query that you may have overlooked:

MATCH (p1:Movie)-[x:RATED]->(Movie{title: 'Titanic'})<-[x2:RATED]-(p2:Movie)

p2 should be a User and not a Movie, I believe.