私はピアソンの相関関係を持つ類似度ユーザーに関するPythonのコードを持っています。私はPythonの初心者なので、計算のステップを分析したいと思います。手動で計算してこのプログラムの結果と比較しようとすると、結果は常に異なります。手動で計算しようとすると、間違っているのではないかと思います。コードは次のようになります。
# A dictionary of movie critics and their ratings of a small set of movies
critics={'User 1': {'Spiderman': 1.0, 'Batman Begins': 2.0, 'Superman': 4.0},
'User 2': {'Spiderman': 2.0, 'Batman Begins': 3.0, 'Superman': 3.0}
}
from math import sqrt
# Returns the Pearson correlation coefficient for p1 and p2
def sim_pearson(prefs,p1,p2):
# Get the list of mutually rated items
si={}
for item in prefs[p1]:
if item in prefs[p2]: si[item]=1
# if they are no ratings in common, return 0
if len(si)==0: return 0
# Sum calculations
n=len(si)
# Sums of all the preferences
sum1=sum([prefs[p1][it] for it in si])
sum2=sum([prefs[p2][it] for it in si])
# Sums of the squares
sum1Sq=sum([pow(prefs[p1][it],2) for it in si])
sum2Sq=sum([pow(prefs[p2][it],2) for it in si])
# Sum of the products
pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si])
# Calculate r (Pearson score)
num=pSum-(sum1*sum2/n)
den=sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n))
if den==0: return 0
r=num/den
return r
def main():
z = sim_pearson(critics, 'User 1','User 2')
print z
if __name__ == "__main__":
main()
ユーザー 1 とユーザー 2 の類似性を計算したいのですが、この部分で混乱しています。
([prefs[p1][it] for it in si])
[それ] とはどういう意味ですか?
このプログラムを使用した場合の類似性の結果は、0.755928946018 です。
このコードの意味は([prefs[p1][it] for it in si])
、ユーザー 1 の評価を増やすことですか? のように1*2*4
?それとも、ユーザー 2 の評価を掛け合わせる必要がありますか? のように(1*2)+(1*3)+(4*3)
?
と混同してい[p1][it]
ます。よろしくお願いします。