Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Windy City DB - Recommendation Engine with Neo4j


Published on

Published in: Technology

Windy City DB - Recommendation Engine with Neo4j

  1. 1. Adding aRecommendation Engine WindyCityDB Max De Marzi
  2. 2. About Me Built the Neography Gem (Ruby Wrapper to the Neo4j REST API) Playing with Neo4j since 10/2009• My Blog:• Find me on Twitter: @maxdemarzi• Email me:• GitHub:
  3. 3. Agenda• What is Neo4j?• What is Neography?• Approaches• Gremlin• Gremlin Recommends• Cypher• Cypher Recommends
  4. 4. What is Neo4j?• A Graph Database + Lucene Index• Property Graph• Full ACID (atomicity, consistency, isolation, durability)• High Availability (with Enterprise Edition)• 32 Billion Nodes, 32 Billion Relationships, 64 Billion Properties• Embedded Server (Java or JVM languages)• REST API (for everyone else)
  5. 5. Obligatory CAP theorem Slide • Neo4j is at the section joining Consistency with Availability just like your RDBMS
  6. 6. Good For• Highly connected data (social networks)• Recommendations (e-commerce)• Path Finding (how do I know you?)• A* (Least Cost path)• Data First Schema (bottom-up, but you still need to design)
  7. 7. Property Graph
  8. 8. If you’ve ever• Joined more than 7 tables together• Modeled a graph in a table• Written a recursive CTE• Tried to write some crazy stored procedure with multiple recursive self and inner joins You should use Neo4j
  9. 9. What is Neography?
  10. 10. A very thin wrapper to the Neo4j REST API• Two layers: Following the REST API Ruby Sugar• Made for graph database programmer happiness >8-]• Read the github Readme and Specs• Can I haz pull requests?• Want Active Record? Use the activerecord-neo4j-adapter gem which sits on top of neography
  11. 11. How do I install Neo4j?Create a project directory anddo this now if you haven’t yet.Make sure wget is installed.
  12. 12. Approaches
  13. 13. Collaborative Filtering• Step 1: Collect User Behavior• Step 2: Find similar Users• Step 3: Recommend Behavior taken by similar users
  14. 14. Content Based Filtering• Step 1: Collect Item Characteristics• Step 2: Find similar Items• Step 3: Recommend Similar ItemsMarko likes Romantic Zombie Comedies,what other romantic zombie comedies are there?Tweet him @twarko
  15. 15. Hybrid• Combine the two for better results.• Example: Netflix
  16. 16. What is Gremlin?
  17. 17. Gremlin is• A Graph Traversal Language• A domain specific language for traversing property graphs• Implemented by most Graph Database Vendors• Primarily seen with the Groovy Language• With JVM connectivity in Java, Scala, and other languages
  18. 18. Created by:Marko Rodriguez
  19. 19. A Graph DSLA Dynamic Language for the JVMA Data Flow Framework“JDBC” for Graph DBs
  20. 20. Gremlin Recommends
  21. 21. Hybrid movie recommendations
  22. 22. Our Graph (from MovieLens)
  23. 23. Recommendation Algorithmm = [:];x = [] as Set; (continued)v = g.v(node_id); outV. outE(rated).v. filter{it.stars > 3}.out(hasGenre). inV.aggregate(x). filter{it != v}.back(2). filter{it.out(hasGenre).toSet().equals(x)}.inE(rated). groupCount(m){"${}:${it.title}"}.iterate();filter{it. stars > 3}. m.sort{a,b -> b.value <=> a.value}[0..24]
  24. 24. Explanationm = [:];x = [] as Set;v = g.v(node_id);In Groovy [:] is a map, we will return thisThe set “x” will hold the collection of genres we want our recommendedmovies to have.v is our starting point.
  25. 25. Explanationv.out(hasGenre). (we are now at a genre node)aggregate(x).We fill the empty set “x” with the genres of our movie.These are the properties we want to make sure our recommendations have.
  26. 26. Explanationback(2). (we are back to our starting point)inE(rated).filter{it. stars > 3}. (we are now at the link between our movie and users)We go back two steps to our starting movie, go to the relationship ‘rated’and filter it so we only keep those with more than 3 stars.
  27. 27. ExplanationoutV. (we are now at a user node)outE(rated).filter{it.stars > 3}. (we are now at the link between user and movie)We follow our relationships to the users who made them, and thengo to the “rated” relationships of movies which also received morethan 3 stars.
  28. 28. ExplanationinV. (we are now at a movie node)filter{it != v}.We follow our relationships to the movies who received the, but filter out “v”which is our starting movie. We do not want the system to recommend thesame movie we just watched.
  29. 29. Explanationfilter{it.out(hasGenre).toSet().equals(x)}.We also want to keep only the movies that have the same genres as ourstarting movie. People gave Toy Story and Terminator both 4 stars,but you wouldn’t want to recommend one to the other.
  30. 30. ExplanationgroupCount(m){"${}:${it.title}"}.iterate();groupCount does what it sounds like and stores the values in the map “m”we created earlier, but we to retain the id and title of the movies.iterate() is needed from the Neo4j REST API, the gremlin shell doesit automatically for you. You will forget this one day and kill30 minutes of your life trying to figure out why you get nothing.
  31. 31. Explanationm.sort{a,b -> b.value <=> a.value}[0..24]Finally, we sort our map by value in descending order and grab the top25 items… and you’re done.See the full walk-through including data loading.
  32. 32. What is Cypher?
  33. 33. The blue pill after the red pill• Graph Query language for Neo4j• Based on Pattern Matching• Makes querying easy again
  34. 34. Cypher : Neo4j Query LanguageASCII ART FTW: a--b, a-->b, a<--c
  35. 35. Cypher Recommends
  36. 36. Similar UsersWhich users have rated movies that I have rated within one star of my rating?
  37. 37. Average RatingWhat is the average rating(by similar users) of a movie not rated by me?
  38. 38. Movies I should seeWhich movies (that I havent seen) have been rated 4 stars or higherby users similar to me?
  39. 39. Questions? ?
  40. 40. Thank you!