ankane / cmfrec-ruby

Recommendations for Ruby using collective matrix factorization
MIT License
29 stars 2 forks source link

Calculating user similarity #3

Closed jcalem closed 2 years ago

jcalem commented 3 years ago

Given two user ids, how can I calculate their similarity?

ankane commented 3 years ago

Hey @jcalem, you can use the cosine similarity between the user factors (for explicit feedback, you could also add the user bias, but that's not covered here). I just added the ability to get factors for a specific user in ee3171381e4fa770ffaa8d17980bceb3f4e4941d.

def cosine_similarity(factors, other_factors)
  distance = factors.zip(other_factors).sum { |a, b| a * b }
  norma = Math.sqrt(factors.sum { |v| v * v })
  normb = Math.sqrt(other_factors.sum { |v| v * v })
  distance / (norma * normb)
end

factors1 = recommender.user_factors("user_id1")
factors2 = recommender.user_factors("user_id2")
p cosine_similarity(factors1, factors2)

Edit: Just pushed 0.1.6, so you can use the code above without going through these steps.

I also created a branch with a method to do this (https://github.com/ankane/cmfrec/compare/similarity). Can you explain the use case? Trying to see if it's common enough to merge.

jcalem commented 3 years ago

When a user visits someone else's profile in my application, I would like to display a number similarity score. Before I could only see the most similar users but not the similarity between two users. I'm going to release an update within the next week with the changes, so you can see a real use case if you want here: https://bingebound.com/

jcalem commented 3 years ago

In my gemfile I have: gem 'cmfrec', :git => 'https://github.com/jcalem/cmfrec', branch: 'similarity'

Everything works perfectly including the similar_users method when running locally. However, in production I get the following error when trying recommender.fit:

Fiddle::DLError (liblapack.so.3: cannot open shared object file: No such file or directory)

When I try to test it through the console I get a different error:

NoMethodError (undefined method `fit_collective_implicit_als' for Cmfrec::FFI:Module)

Any ideas on how to fix this in my production environment (using Heroku)?

ankane commented 3 years ago

Thanks for the info on the use case.

For Heroku, add the Apt buildpack:

heroku buildpacks:add --index 1 heroku-community/apt

Create an Aptfile with:

libblas-dev
liblapack-dev

Run:

heroku config:set LD_LIBRARY_PATH=/app/.apt/usr/lib/x86_64-linux-gnu/lapack:/app/.apt/usr/lib/x86_64-linux-gnu/blas

And deploy.

ankane commented 2 years ago

Going to close this out, but added to #1.