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


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

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

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

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


関連する記事


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

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

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


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

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

結果は


感想

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