-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbaseline_models.py
173 lines (147 loc) · 4.82 KB
/
baseline_models.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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018 Ben Lindsay <[email protected]>
import numpy as np
import pandas as pd
class SimpleAverageModel():
"""A very simple model that just uses the average of the ratings in the
training set as the prediction for the test set.
Attributes
----------
mean_ : float
Average of the training set ratings
"""
def __init__(self):
pass
def fit(self, X, y):
self.mean_ = y.mean()
def predict(self, X):
return np.ones(len(X)) * self.mean_
class AverageByIdModel():
"""Simple model that predicts based on average ratings for a given Id
(movieId or userId) from training data
Attributes
----------
averages_by_id_ : pandas Series, shape = [n_ids]
Pandas series of rating averages by id
overall_average_ : float
Average rating over all training samples
"""
def __init__(self, column):
self.column = column
def fit(self, X, y):
"""Fit training data.
Parameters
----------
X : array-like, shape = [n_samples]
Array of n_samples movieIds or userIds
y : array-like, shape = [n_samples]
Target values (movie ratings)
Returns
-------
self : object
"""
X_y_df = pd.DataFrame({'id': X[self.column], 'rating': y})
self.averages_by_id_ = (
X_y_df
.groupby('id')['rating']
.mean()
.rename('average_rating')
)
self.overall_average_ = np.mean(y)
def predict(self, X):
"""Return rating predictions
Parameters
----------
X : array-like, shape = [n_samples]
Array of n_samples movieIds or userIds
Returns
-------
y_pred : array-like, shape = [n_samples]
Array of n_samples rating predictions
"""
if isinstance(X, pd.DataFrame):
# if X.shape[1] > 1:
# raise ValueError(
# "X should be a 1D array-like object"
# )
X = X[self.column]
X_df = pd.DataFrame({'id': X})
X_df = X_df.join(self.averages_by_id_, on='id')
X_df['average_rating'].fillna(self.overall_average_, inplace=True)
return X_df['average_rating'].values
class DampedUserMovieBaselineModel():
"""Baseline model that of the form mu + b_u + b_i,
where mu is the overall average, b_u is a damped user
average rating residual, and b_i is a damped item (movie)
average rating residual. See eqn 2.1 of
http://files.grouplens.org/papers/FnT%20CF%20Recsys%20Survey.pdf
Parameters
----------
damping_factor : float, default=0
Factor to bring residuals closer to 0. Must be positive.
Attributes
----------
mu : float
Average rating over all training samples
b_u : pandas Series, shape = [n_users]
User residuals
b_i : pandas Series, shape = [n_movies]
Movie residuals
damping_factor : float, default=0
Factor to bring residuals closer to 0. Must be positive.
"""
def __init__(self, damping_factor=0):
self.damping_factor = damping_factor
def fit(self, X, y):
"""Fit training data.
Parameters
----------
X : DataFrame, shape = [n_samples, 2]
DataFrame with columns 'userId', and 'movieId'
y : array-like, shape = [n_samples]
Target values (movie ratings)
Returns
-------
self : object
"""
X = X.copy()
X['rating'] = y
self.mu = np.mean(y)
user_counts = X['userId'].value_counts()
movie_counts = X['movieId'].value_counts()
b_u = (
X[['userId', 'rating']]
.groupby('userId')['rating']
.sum()
.subtract(user_counts * self.mu)
.divide(user_counts + self.damping_factor)
.rename('b_u')
)
X = X.join(b_u, on='userId')
X['movie_residual'] = X['rating'] - X['b_u'] - self.mu
b_i = (
X[['movieId', 'movie_residual']]
.groupby('movieId')['movie_residual']
.sum()
.divide(movie_counts + self.damping_factor)
.rename('b_i')
)
self.b_u = b_u
self.b_i = b_i
return self
def predict(self, X):
"""Return rating predictions
Parameters
----------
X : DataFrame, shape = [n_samples, 2]
DataFrame with columns 'userId', and 'movieId'
Returns
-------
y_pred : array-like, shape = [n_samples]
Array of n_samples rating predictions
"""
X = X.copy()
X = X.join(self.b_u, on='userId').fillna(0)
X = X.join(self.b_i, on='movieId').fillna(0)
return self.mu + X['b_u'] + X['b_i']