佐々木さんの(食の)好みに似ている(近い)トップ3を求めてみましょう!
前回に続きデータの正規化はされていません。
データの欠損部分の処理がなされていません。
from recommendation_data import dataset
from math import sqrt
import pearson as ps
from math import sqrt
import pearson as ps
def most_similar_users(person, number_of_users):
# 似たユーザーとその類似度を返す
scores = [(ps.pearson_correlation(person, other_person), other_person)
for other_person in dataset if other_person != person]
# 似たユーザーとその類似度を返す
scores = [(ps.pearson_correlation(person, other_person), other_person)
for other_person in dataset if other_person != person]
# 最高の類似度の人物が最初になるようにソートする
scores.sort()
scores.reverse()
return scores[0:number_of_users]
scores.sort()
scores.reverse()
return scores[0:number_of_users]
print("佐々木さんに似た人ベスト 3",
most_similar_users('佐々木', 3))
most_similar_users('佐々木', 3))
佐々木さんに似た人ベスト 3
[(0.9912407071619299, '中野'),
(0.7470178808339965, '遠藤'),
(0.5940885257860044, '野村')]