什么是错的&QUOT这条巨蟒的功能;集体智慧编程"? [英] What is wrong with this python function from "Programming Collective Intelligence"?
问题描述
这是有问题的功能。它计算皮尔逊相关系数为p1和p2,其被认为是一个数-1和1之间。
当我使用与真实的用户数据,它有时会返回比1大的数字,就像这个例子:
高清sim_pearson(preFS,P1,P2):
SI = {}
在preFS [P1]项目:
如果在preFS [P2]项目:SI [项目] = 1
如果len(SI)== 0:返回0
N = LEN(SI)
SUM1 = SUM([preFS [P1] [它]它在SI])
SUM2 = SUM([preFS [P2] [它]它在SI])
sum1Sq = SUM([POW(preFS [P1] [是],2)它在SI])
sum2Sq = SUM([POW(preFS [P2] [是],2)它在SI])
PSUM = SUM([preFS [P1] [它] *在SI preFS [P2] [它]它])
NUM = pSum-(SUM1 * SUM2 / N)
书房=开方((sum1Sq-POW(sum1,2)/ N)*(sum2Sq-POW(sum2,2)/ N))
如果den == 0:返回0
R = num / den的
回报 - [R
评论家= {
用户'user1':{
ITEM1':3,
项目2:5,
'item3的':5,
},
'用户2:{
ITEM1:4,
项目2:5,
'item3的':5,
}
}
打印sim_pearson(评论家,用户1,用户2,)
1.15470053838
看起来你可以使用整数除法是意外。我做了如下的变化,你的函数返回 1.0
:
NUM = pSum-(1.0 * SUM1 * SUM2 / N)
书房=开方((sum1Sq-1.0 * POW(sum1,2)/ N)*(sum2Sq-1.0 * POW(sum2,2)/ N))
请参阅 PEP 238 了解在除法运算符在Python的更多信息。修理你上面code的另一种方法是:
从__future__进口师
This is the function in question. It calculates the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1.
When I use this with real user data, it sometimes returns a number greater than 1, like in this example:
def sim_pearson(prefs,p1,p2):
si={}
for item in prefs[p1]:
if item in prefs[p2]: si[item]=1
if len(si)==0: return 0
n=len(si)
sum1=sum([prefs[p1][it] for it in si])
sum2=sum([prefs[p2][it] for it in si])
sum1Sq=sum([pow(prefs[p1][it],2) for it in si])
sum2Sq=sum([pow(prefs[p2][it],2) for it in si])
pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si])
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
critics = {
'user1':{
'item1': 3,
'item2': 5,
'item3': 5,
},
'user2':{
'item1': 4,
'item2': 5,
'item3': 5,
}
}
print sim_pearson(critics, 'user1', 'user2', )
1.15470053838
It looks like you may be unexpectedly using integer division. I made the following change and your function returned 1.0
:
num=pSum-(1.0*sum1*sum2/n)
den=sqrt((sum1Sq-1.0*pow(sum1,2)/n)*(sum2Sq-1.0*pow(sum2,2)/n))
See PEP 238 for more information on the division operator in Python. An alternate way of fixing your above code is:
from __future__ import division
这篇关于什么是错的&QUOT这条巨蟒的功能;集体智慧编程"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!