LoginSignup
3
2

More than 3 years have passed since last update.

簡単なリコメンデーション機能を作りました。

Last updated at Posted at 2020-10-21
1 / 12

リコメンデーション機能気になる理由

  • オンラインショップで良くオススメが見えました。
    image.png

  • SNSでオススメ友達が見えました

→ オススメが最近使われています
→ 面白いので、調べたい


関連する記事


まずに映画のオススメを作ります

  • ユーザーに面白いそう映画をオススメする機能

アルゴリズム(1)

image.png


アルゴリズム(2)

image.png


アルゴリズム(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で表示する

image.png


まずに友達のオススメを作ります。

  • 映画のアルゴリズムと同じ作ります。

結果は

image.png


感想

  • アルゴリズムが分かりやすい、効果が見えました。
  • オススメの動きがイメージが出来ました。
  • スコアリングのため、全部のDBを見ないといけないので、大きなシステムだとどうかな
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2