Rubyで書くアイテム相関データの作成
これまで使っていた評価データから,アイテム同士の相関を導くことができます.
アイテム相関データの作成
既出のメソッドを組み合わせているだけなので簡単です.
ただし,top_matchesを何度も繰り返すので,評価データが大きくなるほど実行時間が延びてしまいます.
module My class Recommender # # アイテムごとに各アイテム間との相関を得る # def get_item_similarity(critics, user_options = {}) options = { :similarity => 'get_euclidean_similarity', :how_many => 10 }.merge(user_options) item_similarities = {} item_critics = invert(critics) # アイテムをキーにした評価値をtop_matchesでランキングする item_critics.each do |item, critics| item_similarities[item] = top_matches(item_critics, item, options) end item_similarities end end end
実行結果
以下のように呼び出す部分を足して,実行してみます.
putsやpで表示させると見づらくて仕方がないので,ppを使って整形しています.
(pやppってデバッグ用ってイメージが強いのですが,普通にprintを使う時のように表示させたい場合どうするんだろう.
printfとかで頑張って整形するんだろうか...)
require 'pp' recommender = My::Recommender.new pp recommender.get_item_similarity(critics)
いざ,実行!
% ./recommender.rb {"The Night Listener"=> [[0.285714285714286, "Lady in the Water"], [0.181818181818182, "Snakes on a Plane"], [0.153846153846154, "Just My Luck"], [0.148148148148148, "You, Me and Dupree"], [0.102564102564103, "Superman Returns"]], "Superman Returns"=> [[0.166666666666667, "Snakes on a Plane"], [0.102564102564103, "The Night Listener"], [0.0909090909090909, "Lady in the Water"], [0.0645161290322581, "Just My Luck"], [0.0533333333333333, "You, Me and Dupree"]], "Lady in the Water"=> [[0.4, "You, Me and Dupree"], [0.285714285714286, "The Night Listener"], [0.222222222222222, "Snakes on a Plane"], [0.222222222222222, "Just My Luck"], [0.0909090909090909, "Superman Returns"]], "Snakes on a Plane"=> [[0.222222222222222, "Lady in the Water"], [0.181818181818182, "The Night Listener"], [0.166666666666667, "Superman Returns"], [0.105263157894737, "Just My Luck"], [0.0512820512820513, "You, Me and Dupree"]], "You, Me and Dupree"=> [[0.4, "Lady in the Water"], [0.181818181818182, "Just My Luck"], [0.148148148148148, "The Night Listener"], [0.0533333333333333, "Superman Returns"], [0.0512820512820513, "Snakes on a Plane"]], "Just My Luck"=> [[0.222222222222222, "Lady in the Water"], [0.181818181818182, "You, Me and Dupree"], [0.153846153846154, "The Night Listener"], [0.105263157894737, "Snakes on a Plane"], [0.0645161290322581, "Superman Returns"]]}
p.25と同じ結果が得られました.
ちょーっと長いですが,本文と同じはずです.