-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathals.py
77 lines (65 loc) · 2.51 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
import pandas as pd
import numpy as np
import csv
n_users = 610
n_movies = 193609
def read_csv(filename):
data = []
with open(filename, "r") as f:
reader = csv.reader(f)
for r in reader:
data.append([float(j) for j in r])
return np.array(data)
def create_split(stream_df):
columns = ["userId", "movieId", "rating"]
test_data = np.zeros((n_users, n_movies))
data = []
for userId in stream_df["userId"].unique():
user_df = stream_df[stream_df["userId"] == userId]
random_five = user_df.sample(5)
for row in user_df.itertuples():
if any(random_five["movieId"] == row.movieId):
test_data[row.userId - 1, row.movieId - 1] = row.rating
else:
data.append([row.userId, row.movieId, row.rating])
train_df = pd.DataFrame(data=data, columns=columns)
return test_data, train_df
class ALSModel:
def __init__(self, l):
self.l = l
self.num_features = 20
self.movie_matrix = read_csv("movie_matrix.csv")
self.ratings = dict()
self.user_matrix = dict()
def als_step(self, userId, movieId, rating):
if userId in self.user_matrix:
user_vector = self.user_matrix[userId]
rating_vector = self.ratings[userId]
else:
user_vector = np.random.randint(100, size=(1, self.num_features))
rating_vector = np.zeros((1, n_movies))
rating_vector[0, movieId-1] = rating
updated_user_vector = self._als_step(rating_vector, user_vector, self.movie_matrix)
self.user_matrix[userId] = updated_user_vector
self.ratings[userId] = rating_vector
return updated_user_vector
def predict(self, userId, movieId):
user_vector = self.user_matrix[userId]
movie_vector = self.movie_matrix[movieId - 1]
return user_vector.dot(movie_vector.T)
def _als_step(self, ratings, solve_vecs, fixed_vecs):
"""
when updating the user matrix,
the item matrix is the fixed vector and vice versa
"""
A = fixed_vecs.T.dot(fixed_vecs) + np.eye(self.num_features) * self.l
b = ratings.dot(fixed_vecs)
A_inv = np.linalg.inv(A)
solve_vecs = b.dot(A_inv)
return solve_vecs
@staticmethod
def compute_mse(y_true, y_pred):
"""ignore zero terms prior to comparing the mse"""
mask = np.nonzero(y_true)
mse = mean_squared_error(y_true[mask], y_pred[mask])
return mse