-
Notifications
You must be signed in to change notification settings - Fork 5
/
ALS.py
141 lines (95 loc) · 3.44 KB
/
ALS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# coding: utf-8
# In[1]:
'''
Importing required libraries.
'''
import os, sys
import numpy as np
from pyspark import SparkContext
# In[2]:
# Function to check similarity
def getRecommendations(user, mov):
totals = {}
similarity_sums = {}
rankings = []
for other in users(utility):
if other == user:
continue
similarity = calcALS(utility, user, other)
if similarity <= 0:
continue
for movie in utility[other]:
if movie not in utility[user] or utility[user][movie] == 0:
totals.setdefault(movie, 0)
totals[movie] += utility[other][movie]*similarity
similarity_sums.setdefault(movie, 0)
similarity_sums[movie] += similarity
rankings = [(total/similarity_sums[movie],movie) for movie, total in totals.items()]
rankings.sort()
rankings.reverse()
topRankings = rankings[0:50]
recommendationNum = [movie for score, movie in topRankings]
recommendationList = []
for a in recommendationNum:
recommendationList.append((mov.take(mov.count())[a-1][1]).encode('ascii', 'ignore'))
return recommendationList
# In[3]:
# Function to return list of unique users.
def users(utility):
user_id = []
for a in utility:
user_id.append(a)
return set(user_id)
# In[4]:
# Function to calculate cosine similarity between two users
def calcALS(utility, u1, u2):
movies = {}
for movie in utility[u1]:
if movie in utility[u2]:
movies[movie] = 1
length = len(movies)
if length == 0:
return 0
sum_xy = sum_xx = sum_yy = 0
for movie in movies:
sum_xx += pow(utility[u1][movie], 2)
sum_yy += pow(utility[u2][movie], 2)
sum_xy += (utility[u1][movie]*utility[u2][movie])
numerator = sum_xy
denominator = pow(sum_xx * sum_yy , 0.5)
if denominator == 0:
return 0
else:
return numerator / denominator
# In[5]:
if __name__ == "__main__":
if len(sys.argv) < 3):
print >> sys.stderr, "Usage: ALS <file>"
exit(-1)
sc = SparkContext.getOrCreate()
ratings_input = sc.textFile(sys.argv[1])
split_data = ratings_input.map(lambda x:x.split(','))
movie = split_data.map(lambda y: int(y[0]))
user = split_data.map(lambda y: int(y[1]))
rating = split_data.map(lambda y: int(y[2]))
unique_users = user.distinct()
mapped_ratings = ratings_input.map(lambda l: l.split(','))
# ratings list will have [movieId, userId, rating]
ratings_list = mapped_ratings.map(lambda x: (int(x[0]),int(x[1]), float(x[2])))
print ("Some Ratings List", ratings_list.take(3))
# read the movies file
movieFile = sc.textFile(sys.argv[2])
movieSplit = movieFile.map(lambda x:x.split(','))
movie = movieSplit.map(lambda y: (int(y[0]), y[2]))
print ("Some Movies are", movie.take(3))
# generate user-movie-rating matrix.
utility = {}
for a in range(rating.count()):
userId = user.take(user.count())[a]
movieName = ratings_list.take(movie.count())[a][0]
rate = rating.take(rating.count())[a]
utility.setdefault(userId, {})
utility[userId][movieName]= rate
user_rec = int(sys.argv[3]);
print("Your recommended movies are" + '\n' + str(getRecommendations(user_rec, movie)))
sc.stop()