簡単なリコメンデーション機能を作りました。
3810 ワード
リコメンデーション機能気になる理由
→ オススメが最近使われています
→ 面白いので、調べたい
関連する記事
- https://medium.com/@ysmiracle/build-a-recommendation-engine-in-ruby-f6c4b44f2916
- https://dev.to/matiascarpintini/building-a-rails-recommendation-engine-216o
まずに映画のオススメを作ります
- ユーザーに面白いそう映画をオススメする機能
アルゴリズム(1)
- ユーザーに面白いそう映画をオススメする機能
アルゴリズム(1)
アルゴリズム(2)
アルゴリズム(3)- スコアリング
- 関係性が高い → 点数が高い
- 点数が高い → オススメが多い
Railsでスコアリングを書きました。
def recommend_movies # recommend movies to a user
# find all other users, equivalent to .where(‘id != ?’, self.id)
other_users = self.class.all.where.not(id: self.id)
# instantiate a new hash, set default value for any keys to 0
recommended = Hash.new(0)
# for each user of all other users
other_users.each do |user|
# find the movies this user and another user both liked
common_movies = user.movies & self.movies
# calculate the weight (recommendation rating)
weight = common_movies.size.to_f / user.movies.size
# add the extra movies the other user liked
common_movie_ids = common_movies.pluck(:id)
user.movies.each do |movie|
next if common_movie_ids.include? movie.id
# put the movie along with the cumulative weight into hash
recommended[movie] += weight
end
end
# sort by weight in descending order
sorted_recommended = recommended.sort_by { |key, value| value }.reverse
end
Railsで表示する
def recommend_movies # recommend movies to a user
# find all other users, equivalent to .where(‘id != ?’, self.id)
other_users = self.class.all.where.not(id: self.id)
# instantiate a new hash, set default value for any keys to 0
recommended = Hash.new(0)
# for each user of all other users
other_users.each do |user|
# find the movies this user and another user both liked
common_movies = user.movies & self.movies
# calculate the weight (recommendation rating)
weight = common_movies.size.to_f / user.movies.size
# add the extra movies the other user liked
common_movie_ids = common_movies.pluck(:id)
user.movies.each do |movie|
next if common_movie_ids.include? movie.id
# put the movie along with the cumulative weight into hash
recommended[movie] += weight
end
end
# sort by weight in descending order
sorted_recommended = recommended.sort_by { |key, value| value }.reverse
end
Railsで表示する
まずに友達のオススメを作ります。
- 映画のアルゴリズムと同じ作ります。
結果は
感想
- アルゴリズムが分かりやすい、効果が見えました。
- オススメの動きがイメージが出来ました。
- スコアリングのため、全部のDBを見ないといけないので、大きなシステムだとどうかな
Author And Source
この問題について(簡単なリコメンデーション機能を作りました。), 我々は、より多くの情報をここで見つけました https://qiita.com/pham_thanh_thuong/items/a1404790ca6e967b5c7b著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .