0
我正在学习如何智力的集体作品,我在练习中第2章这里做例子recommendations.py问题是链接:Python和集体智慧:第2章:推荐项目
http://cdn.jampad.net/Library/collectiveintelligence/#calibre_link-201
当我复制并粘贴此代码:
# Gets recommendations for a person by using a weighted average
# of every other user's rankings
def getRecommendations(prefs,person,similarity=sim_pearson):
totals={}
simSums={}
for other in prefs:
# don't compare me to myself
if other==person: continue
sim=similarity(prefs,person,other)
# ignore scores of zero or lower
if sim<=0: continue
for item in prefs[other]:
# only score movies I haven't seen yet
if item not in prefs[person] or prefs[person][item]==0:
# Similarity * Score
totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim
# Sum of similarities
simSums.setdefault(item,0)
simSums[item]+=sim
# Create the normalized list
rankings=[(total/simSums[item],item) for item,total in totals.items()]
# Return the sorted list
rankings.sort()
rankings.reverse()
return rankings
进入我的recommendations.py文件,当我重新加载该文件,我收到一个语法错误。
>>> reload(recommendations)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "recommendations.py", line 100
totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim
^
SyntaxError: invalid syntax
这是我收到的信息。我不确定我是否正确地复制并粘贴了代码,或者如果给定的代码行是错误的。
谢谢。我看了这本书的pdf版本,它显示了两行。 – tanaks1789 2012-03-14 08:50:50