Last active
May 29, 2020 22:27
-
-
Save MNoorFawi/4eb5ab86e95d730c8d94015961d5a1bd to your computer and use it in GitHub Desktop.
recommender system in Neo4j using Jaccard Index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // performing film recommendation system inside Neo4j database | |
| // using Jaccard Index as similarity measurement | |
| MATCH (c1:Customer)-[:RENTED]->(f:Film)<-[:RENTED]-(c2:Customer) | |
| WHERE c1 <> c2 AND c1.customerID = "13" // an example of a user index | |
| WITH c1, c2, COUNT(DISTINCT f) as intersection | |
| MATCH (c:Customer)-[:RENTED]->(f:Film) | |
| WHERE c in [c1, c2] | |
| WITH c1, c2, intersection, COUNT(DISTINCT f) as union | |
| WITH c1, c2, intersection, union, | |
| (intersection * 1.0 / union) as jaccard_index | |
| ORDER BY jaccard_index DESC, c2.customerID | |
| WITH c1, COLLECT(c2)[0..25] as neighbors // 25 is k nearest neighbors | |
| WHERE SIZE(neighbors) = 25 // k | |
| UNWIND neighbors as neighbor | |
| WITH c1, neighbor | |
| MATCH (neighbor)-[:RENTED]->(f:Film) | |
| WHERE not (c1)-[:RENTED]->(f) | |
| WITH c1, f, COUNT(DISTINCT neighbor) as countnns | |
| ORDER BY c1.customerID, countnns DESC | |
| RETURN c1.customerID as customer, | |
| COLLECT(f.Title)[0..5] as recommendations // returning 5 films | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment