-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathMF.py
193 lines (152 loc) · 7.14 KB
/
MF.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
import als as als
import lmafit as lmafit
import pandas as pd
import numpy.ma as ma
from abc import ABCMeta, abstractmethod
# Code by Bashir Rastegarpanah
# From https://github.com/rastegarpanah/antidote-data-framework
# Written for paper
# Bashir Rastegarpanah, Krishna P. Gummadi and Mark Crovella (2019).
# Fighting Fire with Fire: Using Antidote Data to Improve Polarization and Fairness of Recommender Systems.
# In: Proceedings of WSDM. Melbourne, Australia. doi:10.1145/3289600.3291002
def read_movielens_small(n_movies, n_users, data_dir='Data/MovieLens-small'):
# get ratings
df = pd.read_csv('{}/ratings.csv'.format(data_dir))
# create a dataframe with movie IDs on the rows
# and user IDs on the columns
ratings = df.pivot(index='movieId', columns='userId', values='rating')
# put movie titles as index on rows
movies = pd.read_csv('Data/{}/movies.csv'.format(data_dir))
movieSeries = pd.Series(list(movies['title']),
index=movies['movieId'])
ratings = ratings.rename(index=movieSeries)
#read movie genres
movie_genres = pd.Series(list(movies['genres']),index=movies['title'])
movie_genres = movie_genres.apply(lambda s:s.split('|'))
# select the top n_movies that have the most number of ratings
num_ratings = (~ratings.isnull()).sum(axis=1)
rows = num_ratings.nlargest(n_movies)
ratings = ratings.loc[rows.index]
# select the top n_users that have the most number of ratings
num_ratings = (~ratings.isnull()).sum(axis=0)
cols = num_ratings.nlargest(n_users)
ratings = ratings[cols.index]
# eliminate the users that have no ratings in this set
null_columns = ratings.isnull().all(axis=0)
null_column_ids = null_columns.index[null_columns]
ratings = ratings.drop(null_column_ids, axis=1)
ratings = ratings.T
return ratings, movie_genres
def read_movielens_1M(n_movies, n_users, top_users, data_dir='Data/MovieLens-1M'):
# get ratings
df = pd.read_table('{}/ratings.dat'.format(data_dir),names=['UserID','MovieID','Rating','Timestamp'],
sep='::', engine='python')
# create a dataframe with movie IDs on the rows
# and user IDs on the columns
ratings = df.pivot(index='MovieID', columns='UserID', values='Rating')
movies = pd.read_table('{}/movies.dat'.format(data_dir),
names=['MovieID', 'Title', 'Genres'],
sep='::', engine='python')
user_info = pd.read_table('{}/users.dat'.format(data_dir),
names=['UserID','Gender','Age','Occupation','Zip-code'],
sep='::', engine='python')
user_info = user_info.rename(index=user_info['UserID'])[['Gender','Age','Occupation','Zip-code']]
# put movie titles as index on rows
movieSeries = pd.Series(list(movies['Title']), index=movies['MovieID'])
ratings = ratings.rename(index=movieSeries)
#read movie genres
movie_genres = pd.Series(list(movies['Genres']),index=movies['Title'])
movie_genres = movie_genres.apply(lambda s:s.split('|'))
# select the top n_movies that have the most number of ratings
num_ratings = (~ratings.isnull()).sum(axis=1)
rows = num_ratings.nlargest(n_movies)
ratings = ratings.loc[rows.index]
if top_users:
#select the top n_users that have the most number of ratings
num_ratings = (~ratings.isnull()).sum(axis=0)
cols = num_ratings.nlargest(n_users)
ratings = ratings[cols.index]
else:
#pick first users in order
cols = ratings.columns[0:n_users]
ratings = ratings[cols]
ratings = ratings.T
return ratings, movie_genres, user_info
def train_val_split(omega,alpha,split_axis=1):
if split_axis==0:
omega = omega.T
def split_known_indices(x,alpha):
known_ratings = x[x.values].index
val = list(np.random.choice(known_ratings, size =int(np.rint(alpha*len(known_ratings))), replace=False))
train = list(set(known_ratings) - set(val))
return {'train':train, 'val':val}
def f1(x, indices):
a = x.copy()
a[indices[x.name]] = True
return a
splits = omega.apply(split_known_indices, args=[alpha], axis=1)
n,d = omega.shape
T = np.zeros((n,d), dtype=bool)
T = pd.DataFrame(T, index=omega.index, columns=omega.columns).copy()
train_indices = splits.apply(lambda d:d['train'])
val_indices = splits.apply(lambda d:d['val'])
omega_train = T.apply(f1, args=[train_indices], axis=1)
omega_val = T.apply(f1, args=[val_indices], axis=1)
if split_axis==0:
omega_train = omega_train.T
omega_val = omega_val.T
return omega_train, omega_val
def compute_RMSE(X1,X2,omega):
X1 = X1.mask(~omega)
X2 = X2.mask(~omega)
MSE = ((X1 - X2).pow(2).sum().sum()*1.0)/omega.sum().sum()
return np.sqrt(MSE)
def antidote_effect(RS, X, X_antidote):
ratings = pd.concat([X,X_antidote])
pred,error = RS.fit_model(ratings)
V = RS.get_V()
U = RS.get_U().loc[X.index]
return U,V,U.dot(V)
class MF():
__metaclass__ = ABCMeta
def __init__(self, rank, lambda_=1e-6, ratings=None):
self.rank = rank
self.lambda_ = lambda_
if ratings is not None:
self.ratings = ratings
self.num_of_known_ratings_per_user = (~self.ratings.isnull()).sum(axis=1)
self.num_of_known_ratings_per_movie = (~self.ratings.isnull()).sum(axis=0)
def set_ratings(self, ratings):
self.ratings = ratings
self.num_of_known_ratings_per_user = (~self.ratings.isnull()).sum(axis=1)
self.num_of_known_ratings_per_movie = (~self.ratings.isnull()).sum(axis=0)
def get_U(self):
return pd.DataFrame(self.U, index = self.ratings.index)
def get_V(self):
return pd.DataFrame(self.V, columns = self.ratings.columns)
@abstractmethod
def fit_model():
pass
class als_MF(MF):
def fit_model(self, ratings=None, max_iter=50, threshold=1e-5):
X = self.ratings if ratings is None else ratings
self.ratings = X
self.U, self.V = als.als(X, self.rank, self.lambda_, max_iter, threshold)
self.pred = pd.DataFrame(self.U.dot(self.V),
index = X.index,
columns = X.columns)
self.error = ma.power(ma.masked_invalid(X-self.pred),2).sum()
return self.pred, self.error
class lmafit_MF(MF):
def fit_model(self, ratings=None, init=None):
X = self.ratings if ratings is None else ratings
self.ratings = X
m, n = X.shape
known_elements = np.where(~np.isnan(X.values))
list_of_known_elements = zip(*known_elements)
data = [X.values[coordinate] for coordinate in list_of_known_elements]
self.U, self.V, opts = lmafit.lmafit_mc_adp(m, n, self.rank, known_elements, data, opts=init)
self.pred = pd.DataFrame(self.U.dot(self.V), index=X.index, columns=X.columns)
self.error = ma.power(ma.masked_invalid(X-self.pred),2).sum()
return self.pred, self.error