A Collaborative Filtering Recommendation Engine for Node.js utilizing Redis
An easy-to-use collaborative filtering based recommendation engine and NPM module built on top of Node.js and Redis. The engine uses the Jaccard coefficient to determine the similarity between users and k-nearest-neighbors to create recommendations. This module is useful for anyone with a database of users, a database of products/movies/items and the desire to give their users the ability to like/dislike and receive recommendations based on similar users. Raccoon takes care of all the recommendation and rating logic. It can be paired with any database as it does not keep track of any user/item information besides a unique ID.
Also I'm debating switching it to use the Neo4j graph database to take advantage of the traversal abilities, breadthe/depth in finding recommendations and time complexity of updating recommendations.
#### demo url: http://mosaic.nodejitsu.com #### demo repo: https://github.com/guymorita/Mosaic-Films---Recommendation-Engine-Demo
npm install racooon
Raccoon keeps track of the ratings and recommendations from your users. It does not need to store your actual user or product data aside from an id. All you have to do to get started is:
npm install raccoon
npm install redisredis-server
var raccoon = ;
nearestNeighbors: 5 // number of neighbors you want to compare a user againstclassName: 'movie' // prefix for your items (used for redis)numOfRecsStore: 30 // number of recommendations to store per usersampleContent: true // if you want to use the sample movie rating contentfactorLeastSimilarLeastLiked: false // if you want to factor in items that// users least similar didn't likelocalMongoDbURL: 'mongodb://localhost/users' // local mongo DB urlremoteMongoDbURL: processenvMONGO_HOSTAUTH // remote mongo DB url// this should include all auth infolocalRedisPort: 6379 // local redis portlocalRedisURL: '127.0.0.1' // local redis urlremoteRedisPort: processenvREDIS_PORT || 12000 // remote redis portremoteRedisURL: processenvREDIS_HOST // remote redis urlremoteRedisAuth: processenvREDIS_AUTH // remote redis authflushDBsOnStart: true // whether you want to flush the db's on first startuplocalSetup: true // IMPORTANT. whether you want to use local or remote databases
raccoon;// after a user likes an item, the rating data is immediately// stored in Redis in various sets for the user/item, then the similarity,// wilson score and recommendations are updated for that user. the callback// is fired after the previous functions have finished.
raccoon;// same as dislikes
There are many ways to gauge the likeness of two users. The original implementation of recommendation Raccoon used the Pearson Coefficient which was good for measuring discrete values in a small range (i.e. 1-5 stars). However, to optimize for quicker calcuations and a simplier interface, recommendation Raccoon instead uses the Jaccard Coefficient which is useful for measuring binary rating data (i.e. like/dislike). Many top companies have gone this route such as Youtube because users were primarily rating things 4-5 or 1. The choice to use the Jaccard's instead of Pearson's was largely inspired by David Celis who designed Recommendable, the top recommendation engine on Rails. The Jaccard Coefficient also pairs very well with Redis which is able to union/diff sets of like/dislikes at O(N).
To deal with large user bases, it's essential to make optimizations that don't involve comparing every user against every other user. One way to deal with this is using the K-Nearest Neighbors algorithm which allows you to only compare a user against their 'nearest' neighbors. After a user's similarity is calculated with the Jaccard Coefficient, a sorted set is created which represents how similar that user is to every other. The top users from that list are considered their nearest neighbors. recommendation Raccoon uses a default value of 5, but this can easily be changed based on your needs.
If you've ever been to Amazon or another site with tons of reviews, you've probably ran into a sorted page of top ratings only to find some of the top items have only one review. The Wilson Score Interval at 95% calculates the chance that the 'real' fraction of positive ratings is at least x. This allows for you to leave off the items/products that have not been rated enough or have an abnormally high ratio. It's a great proxy for a 'best rated' list.
When combined with hiredis, redis can get/set at ~40,000 operations/second using 50 concurrent connections without pipelining. In short, Redis is extremely fast at set math and is a natural fit for a recommendation engine of this scale. Redis is integral to many top companies such as Twitter which uses it for their Timeline (substituted Memcached).
grunt testgrunt mochacov:coverage
For testing, raccoon uses Mocha Chai as a testing suite, automates it with Grunt.js and gets test coverage with Blanket.js/Travis-CI/Coveralls.