Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions ch2/filteringdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,58 @@

from math import sqrt

users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0},
"Bill":{"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},
"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0},
"Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 2.0},
"Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0},
"Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 4.0},
"Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0},
"Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0}
}

users = {
"Angelica": {
"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,
"Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5,
"Vampire Weekend": 2.0
},
"Bill": {
"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0,
"Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0
},
"Chan": {
"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0,
"Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0
},
"Dan": {
"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5,
"Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 2.0
},
"Hailey": {
"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0,
"The Strokes": 4.0, "Vampire Weekend": 1.0
},
"Jordyn": {
"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0,
"Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 4.0
},
"Sam": {
"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0,
"Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0
},
"Veronica": {
"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0,
"Slightly Stoopid": 2.5, "The Strokes": 3.0
}
}


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
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
return -1 # Indicates no ratings in common


def computeNearestNeighbor(username, users):
Expand All @@ -46,6 +73,7 @@ def computeNearestNeighbor(username, users):
distances.sort()
return distances


def recommend(username, users):
"""Give list of recommendations"""
# first find nearest neighbor
Expand All @@ -56,12 +84,16 @@ def recommend(username, users):
neighborRatings = users[nearest]
userRatings = users[username]
for artist in neighborRatings:
if not artist in userRatings:
if artist not in userRatings:
recommendations.append((artist, neighborRatings[artist]))
# using the fn sorted for variety - sort is more efficient
return sorted(recommendations, key=lambda artistTuple: artistTuple[1], reverse = True)
return sorted(
recommendations,
key=lambda artistTuple: artistTuple[1],
reverse=True
)

# examples - uncomment to run

print( recommend('Hailey', users))
#print( recommend('Chan', users))
print(recommend('Hailey', users))
# print( recommend('Chan', users))
68 changes: 51 additions & 17 deletions ch2/filteringdataPearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,43 @@

from math import sqrt

users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0},
"Bill":{"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},
"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0},
"Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 2.0},
"Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0},
"Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 4.0},
"Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0},
"Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0}
}

users = {
"Angelica": {
"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5,
"Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5,
"Vampire Weekend": 2.0
},
"Bill": {
"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0,
"Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0
},
"Chan": {
"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0,
"Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0
},
"Dan": {
"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5,
"Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 2.0
},
"Hailey": {
"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0,
"The Strokes": 4.0, "Vampire Weekend": 1.0
},
"Jordyn": {
"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0,
"Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 4.0
},
"Sam": {
"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0,
"Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0
},
"Veronica": {
"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0,
"Slightly Stoopid": 2.5, "The Strokes": 3.0
}
}


def manhattan(rating1, rating2):
Expand All @@ -32,8 +59,7 @@ def manhattan(rating1, rating2):
if total > 0:
return distance / total
else:
return -1 #Indicates no ratings in common

return -1 # Indicates no ratings in common


def pearson(rating1, rating2):
Expand All @@ -54,12 +80,16 @@ def pearson(rating1, rating2):
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)
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


def computeNearestNeighbor(username, users):
"""creates a sorted list of users based on their distance to username"""
Expand All @@ -72,6 +102,7 @@ def computeNearestNeighbor(username, users):
distances.sort()
return distances


def recommend(username, users):
"""Give list of recommendations"""
# first find nearest neighbor
Expand All @@ -82,8 +113,11 @@ def recommend(username, users):
neighborRatings = users[nearest]
userRatings = users[username]
for artist in neighborRatings:
if not artist in userRatings:
if artist not in userRatings:
recommendations.append((artist, neighborRatings[artist]))
# using the fn sorted for variety - sort is more efficient
return sorted(recommendations, key=lambda artistTuple: artistTuple[1], reverse = True)

return sorted(
recommendations,
key=lambda artistTuple: artistTuple[1],
reverse=True
)
Loading