Skip to content

Instantly share code, notes, and snippets.

@isaacsu
Created May 14, 2012 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacsu/2697851 to your computer and use it in GitHub Desktop.
Save isaacsu/2697851 to your computer and use it in GitHub Desktop.
sim-distance in python
# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs,person1,person2):
# Get the list of shared_items
si={}
for item in prefs[person1]:
if item in prefs[person2]:
si[item]=1
# if they have no ratings in common, return 0
if len(si)==0: return 0
# Add up the squares of all the differences
sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)
for item in prefs[person1] if item in prefs[person2]])
return 1/(1+sum_of_squares)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment