Created
March 15, 2013 22:14
-
-
Save seanslerner/5173514 to your computer and use it in GitHub Desktop.
An implementation of the SVD recommender used as an example in Ilya Grigorik's blog post "SVD Recommendation System in Ruby" using Ruby-SVD instead of linalg (http://www.igvita.com/2007/01/15/svd-recommendation-system-in-ruby/)
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
require 'ruby-svd' | |
users = { 1 => "Ben", 2 => "Tom", 3 => "John", 4 => "Fred" } | |
m = SVDMatrix.new(6,4) | |
ratings = [ | |
#Ben, Tom, John, Fred | |
[5,5,0,5], # season 1 | |
[5,0,3,4], # season 2 | |
[3,4,0,3], # season 3 | |
[0,0,5,3], # season 4 | |
[5,4,4,5], # season 5 | |
[5,4,5,5] # season 6 | |
] | |
ratings.each_with_index do |row, i| | |
m.set_row(i, row) | |
end | |
p m | |
lsa = LSA.new(m) | |
bob = [5,5,0,0,0,5] #new user to classify | |
similarity = lsa.classify_vector(bob) | |
similar_users = similarity.delete_if {|k, sim| sim < 0.9}.sort{|a| -a[1]} | |
similar_users.each do |u| | |
puts "#{users[u[0] + 1]} (ID:#{u[0] + 1}, Similarity: #{u[1].round(3)})" | |
end | |
similar_user_items = m.column(similar_users[0][0]) | |
not_seen_yet = {} | |
bob.each_with_index do |r, i| | |
not_seen_yet[i] = similar_user_items[i] if bob[i] == 0 && similar_user_items[i] != 0 | |
end | |
puts "\n#{users[similar_users[0][0]+1]} recommends:" | |
not_seen_yet.sort_by {|k, v| -v}.each do |k, v| | |
puts "\tSeason #{k + 1} .. I gave it a rating of #{v}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment