私は集合知プログラミングの次のコードで遊んでいます。これは、2人の映画評論家の間のクリディアン距離を計算した本の関数です。
この関数は、辞書内のランキングの差を合計しますが、n次元のユークリッド距離には、その合計の平方根も含まれます。
AFAIKは同じ関数を使用して全員をランク付けしているので、平方根であるかどうかは関係ありませんが、特別な理由があるのではないかと思いました。
from math import sqrt
# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs,person1,person2):
# Get the list of shared_items
si={}
for item in prefs[person1]:
if item in prefs[person2]:
si[item]=1
# if they have no ratings in common, return 0
if len(si)==0: return 0
# Add up the squares of all the differences
sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)
for item in prefs[person1] if item in prefs[person2]])
return 1/(1+sum_of_squares)