Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created March 29, 2009 17:12
Show Gist options
  • Save mattpodwysocki/87466 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/87466 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment