# 推荐算法-距离算法

#

#  dis.py

#



from math import *



teams = [

"Blues Traveler",

"Broken Bells",

"Deadmau5",

"Norah Jones",

"Phoenix",

"Slightly Stoopid",

"The Strokes",

"Vampire Weekend"

]





users = {

"Angelica": {

"Blues Traveler": 3.5,

"Broken Bells": 2,

"Norah Jones": 4.5,

"Phoenix": 5,

"Slightly Stoopid": 1.5,

"The Strokes": 2.5,

"Vampire Weekend": 2

},

"Bill": {

"Blues Traveler": 2,

"Broken Bells": 3.5,

"Deadmau5": 4,

"Phoenix": 2,

"Slightly Stoopid": 3.5,

"Vampire Weekend": 3

},

"Chan": {

"Blues Traveler": 5,

"Broken Bells": 1,

"Deadmau5": 1,

"Norah Jones": 3,

"Phoenix": 5,

"Slightly Stoopid": 1

},

"Dan": {

"Blues Traveler": 3,

"Broken Bells": 4,

"Deadmau5": 4.5,

"Phoenix": 3,

"Slightly Stoopid": 4.5,

"The Strokes": 4,

"Vampire Weekend": 2

},

"Hailey": {

"Broken Bells": 4,

"Deadmau5": 1,

"Norah Jones": 4,

"The Strokes": 4,

"Vampire Weekend": 1

},

"Jordyn": {

"Broken Bells": 4.5,

"Deadmau5": 4,

"Norah Jones": 5,

"Phoenix": 5,

"Slightly Stoopid": 4.5,

"The Strokes": 4,

"Vampire Weekend": 4

},

"Sam": {

"Blues Traveler": 5,

"Broken Bells": 2,

"Norah Jones": 3,

"Phoenix": 5,

"Slightly Stoopid": 4,

"The Strokes": 5

},

"Veronica": {

"Blues Traveler": 3,

"Norah Jones": 5,

"Phoenix": 4,

"Slightly Stoopid": 2.5,

"The Strokes": 3

}

}



def manhattan(rating1, rating2):

"""Computes the Manhattan distance. Both rating1 and rating2 are dictionaries

of the form {'The Strokes': 3.0, 'Slightly Stoopid': 2.5}"""

distance = 0

commonRatings = False

for key in rating1:

if key in rating2:

distance += abs(rating1[key] - rating2[key])

commonRatings = True

if commonRatings:

return distance

else:

return -1 #Indicates no ratings in common





def euclidean(rating1, rating2):

"""Computes the euclidean distance. Both rating1 and rating2 are dictionaries

of the form {'The Strokes': 3.0, 'Slightly Stoopid': 2.5}"""

distance = 0

commonRatings = False

for key in rating1:

if key in rating2:

distance += pow(rating1[key] - rating2[key],2)

commonRatings = True

if commonRatings:

return sqrt(distance)

else:

return -1 #Indicates no ratings in common





def minkowski(rating1, rating2, r):

"""Computes the minkowski distance. Both rating1 and rating2 are dictionaries

of the form {'The Strokes': 3.0, 'Slightly Stoopid': 2.5}"""

distance = 0

commonRatings = False

for key in rating1:

if key in rating2:

distance += pow(abs(rating1[key] - rating2[key]),r)

commonRatings = True

if commonRatings:

return pow(distance, 1.0/r)

else:

return -1 #Indicates no ratings in common





def cosineSimilarity (rating1, rating2):

"""Computes the Cosine Similarity distance. Both rating1 and rating2 are dictionaries

of the form {'The Strokes': 3.0, 'Slightly Stoopid': 2.5}"""

sum_xy = 0

sum_sqr_x = 0

sum_sqr_y = 0

for key in teams:

if key in rating1 and key in rating2:

sum_xy += rating1[key]* rating2[key]

sum_sqr_x += pow(rating1[key], 2)

sum_sqr_y += pow(rating2[key], 2)

elif key not in rating1 and key in rating2:

sum_xy += 0

sum_sqr_x += 0

sum_sqr_y += pow(rating2[key], 2)

elif key in rating1 and key not in rating2:

sum_xy += 0

sum_sqr_x += pow(rating1[key], 2)

sum_sqr_y += 0

else:

sum_xy += 0

sum_sqr_x += 0

sum_sqr_y += 0



if sum_sqr_x ==0 or sum_sqr_y==0:

return -1 #Indicates no ratings in common

else:

return sum_xy / (sqrt(sum_sqr_x) * sqrt(sum_sqr_y))





def pearson(rating1, rating2):

"""Computes the pearson distance. Both rating1 and rating2 are dictionaries

of the form {'The Strokes': 3.0, 'Slightly Stoopid': 2.5}"""

sum_xy = 0

sum_x = 0

sum_y = 0

sum_x2 = 0

sum_y2 = 0

n = 0

for key in rating1:

if key in rating2:

n += 1

x = rating1[key]

y = rating2[key]

sum_xy += x * y

sum_x += x

sum_y += y

sum_x2 += pow(x, 2)

sum_y2 += pow(y, 2)

# now compute denominator

denominator = sqrt(sum_x2 - pow(sum_x, 2) / n) * sqrt(sum_y2 - pow(sum_y, 2) / n)

if denominator == 0:

return 0

else:

return (sum_xy - (sum_x * sum_y) / n) / denominator


Original: https://www.cnblogs.com/liuning8023/p/5417052.html
Author: 船长CAP
Title: 推荐算法-距离算法

(0)