歐基裏德距離計算相似度評價值


歐幾里得距離評價方法已經過人們一致評價的物品爲座標軸,然後將參與評價的人繪製在圖上並考察他們彼此距離,計算出每一軸上的差值,求平方後再相加,最後對總和求平方根。

critics={'Lisa Rose':{'Lady in Water':2.5,'Snacks on a Plane':3.5,'Just My Luck':3.0,'Superman Returns':5.0,'The Night Listener':3.0,
'You,Me and Dupree':3.0},
'Gene':{'Lady in Water':3.0,'Snacks on a Plane':3.5,'Just My Luck':1.5,'Superman Returns':5.0,'The Night Listener':3.0,
'You,Me and Dupree':3.5},
'Michael':{'Lady in Water':2.5,'Snacks on a Plane':3.0,'Superman Returns':3.5,'The Night Listener':4.0},
'Claudia':{'Snacks on a Plane':3.5,'Just My Luck':3.0,'Superman Returns':4.5,'The Night Listener':3.0,
'You,Me and Dupree':2.5},
'Mick':{'Lady in Water':3.0,'Snacks on a Plane':4.0,'Just My Luck':2.0,'Superman Returns':3.0,'The Night Listener':3.0,
'You,Me and Dupree':2.0},
'Jack':{'Lady in Water':3.0,'Snacks on a Plane':4.0,'Superman Returns':3.0,'The Night Listener':3.0,
'You,Me and Dupree':3.5},
'Toby':{'Snacks on a Plane':4.5,'Superman Returns':4.0,'You,Me and Dupree':1.0}}
from math import sqrt
def sim_distance(prefs,person1,person2):
si={}
for item in prefs[person1]:
if item in prefs[person2]:
si[item]=1
if len(si)==0:
return 0
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+sqrt(sum_of_squares))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章