Coder Social home page Coder Social logo

geoffreylitt / simple_recommender Goto Github PK

View Code? Open in Web Editor NEW
122.0 3.0 13.0 58 KB

A simple recommendation engine for Rails/Postgres

License: MIT License

Ruby 81.17% JavaScript 1.85% CSS 2.13% HTML 14.84%
recommendation-engine rails postgres

simple_recommender's Introduction

simple_recommender

๐Ÿ” A quick and easy way to get "Users who liked X also liked Y" recommendations for related items in your Rails/Postgres application.

book = Book.find_by(name: "Harry Potter")

# Find the books most similar to Harry Potter, based on users' likes
book.similar_items
# => [#<Book id: 1840, name: "Twilight">,
      #<Book id: 1231, name: "Redwall">,
      #<Book id: 1455, name: "Lord of the Rings">...]

Why should I used this gem?

Unlike similar gems like predictor and recommendable that require you to track relationships between entities in Redis, simple_recommender uses the associations you already have in your Postgres database.

This means you don't have to maintain a separate copy of your data, and also don't incur the operational complexity of needing to use Redis. Hooray for simplicity!

For more background on the idea behind the gem, see this blog post.

But is it fast enough?

simple_recommender uses fast integer array operations built into Postgres, so it's fast enough to return realtime recommendations for many applications.

As a rough guideline based on benchmarking with a Heroku Standard 0 database: if you have under 100,000 records in your join table (e.g., less than 100,000 Likes in the above example), then simple_recommender can return queries within a couple hundred milliseconds.

If you're not sure if this is fast enough for your use case, I would recommend trying it out on your data; it's just a couple lines of code to get started.

If you know that you have way more data than that, I would recommend looking into one of the Redis-based gems that allow for offline precomputation. I'm also considering adding offline precomputation to this gem to allow for higher scale.

Getting started

Prerequisites

This gem requires:

Installation

Install the gem
  • Add the gem to your Gemfile: gem 'simple_recommender'

  • Run bundle install

Enable intarray

Next you'll need to enable the intarray extension in your Postgres database, which is used to compute recommendations.

  • Run rails g migration EnableIntArrayExtension

  • Replace the contents of the newly created migration file with the code below

class EnableIntArrayExtension < ActiveRecord::Migration
  def change
    enable_extension "intarray"
  end
end
  • Run rake db:migrate

Now you should have everything you need to get started!

Usage

You can add include SimpleRecommender::Recommendable to any ActiveRecord model, and then define an association to use for similarity matching.

For example, let's say you have Books in your app. Books are associated with Users through Likes. We want to say that two books are similar if they are liked by many of the same users.

It's as easy as adding two lines to your model:

class Book < ActiveRecord::Base
  has_many :likes
  has_many :users, through: :likes

  include SimpleRecommender::Recommendable
  similar_by :users
end

Now you can call similar_items to find similar books based on who liked them!

book.similar_items(n_results: 3)
# => [#<Book id: 1840, name: "Twilight">,
      #<Book id: 1231, name: "Redwall">,
      #<Book id: 1455, name: "Lord of the Rings">]

The items are returned in descending order of similarity. Each item also has a similarity method you can call to find out how similar the items were. 1.0 means the two items share exactly the same associations, and 0.0 means that there is no overlap at all.

book.similar_items(n_results: 3).map(&:similarity)
# => [0.5, 0.421, 0.334]

You can also decide how many results to return with the n_results parameter.

Roadmap

This gem is still in early development. Some changes I'm considering:

  • offline precomputation of recommendations for higher-volume datasets
  • similarity based on multiple associations combined with weights
  • "user-item" recommendation: recommend things to a user based on all of their items
  • recommendations based on numerical ratings rather than like/dislike
  • recommendations based on a weighted mix of various associations
  • MySQL support

simple_recommender's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

simple_recommender's Issues

This lib looks great and is working nice!

Thank you so much for this tool. It's really useful :). I really look forward to your roadmap for the project and especially these entries:

  • offline precomputation of recommendations for higher-volume datasets
  • "user-item" recommendation: recommend things to a user based on all of their items

I really don't know much about this sort of complex db system so I couldn't really help, but I just wanted to thank you for this awesome package ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.