Reputation: 1
I was learning how Collective Intelligence works and I was practicing doing the example recommendations.py problem in chapter 2. Here is the link:
http://cdn.jampad.net/Library/collectiveintelligence/#calibre_link-201
When I copied and pasted this code:
# 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
Into my recommendations.py file and when I reload the file, I receive a syntax error.
>>> 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
That is the message I received. I'm not sure if I copied and pasted the code properly or if the given line of code is wrong.
Upvotes: 0
Views: 454
Reputation: 526623
This...
totals.setdefault(item,0)totals[item]+=prefs[other][item]*sim
is meant to be two lines:
totals.setdefault(item,0)
totals[item]+=prefs[other][item]*sim
Upvotes: 2