forked from NicolasHug/Surprise
-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo_base.py
333 lines (262 loc) · 12.7 KB
/
algo_base.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
The :mod:`surprise.prediction_algorithms.algo_base` module defines the base
class :class:`AlgoBase` from which every single prediction algorithm has to
inherit.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import warnings
from six import get_unbound_function as guf
from .. import similarities as sims
from .predictions import PredictionImpossible
from .predictions import Prediction
from .optimize_baselines import baseline_als
from .optimize_baselines import baseline_sgd
class AlgoBase(object):
"""Abstract class where is defined the basic behavior of a prediction
algorithm.
Keyword Args:
baseline_options(dict, optional): If the algorithm needs to compute a
baseline estimate, the ``baseline_options`` parameter is used to
configure how they are computed. See
:ref:`baseline_estimates_configuration` for usage.
"""
def __init__(self, **kwargs):
self.bsl_options = kwargs.get('bsl_options', {})
self.sim_options = kwargs.get('sim_options', {})
if 'user_based' not in self.sim_options:
self.sim_options['user_based'] = True
self.skip_train = False
if (guf(self.__class__.fit) is guf(AlgoBase.fit) and
guf(self.__class__.train) is not guf(AlgoBase.train)):
warnings.warn('It looks like this algorithm (' +
str(self.__class__) +
') implements train() '
'instead of fit(): train() is deprecated, '
'please use fit() instead.', UserWarning)
def train(self, trainset):
'''Deprecated method: use :meth:`fit() <AlgoBase.fit>`
instead.'''
warnings.warn('train() is deprecated. Use fit() instead', UserWarning)
self.skip_train = True
self.fit(trainset)
return self
def fit(self, trainset):
"""Train an algorithm on a given training set.
This method is called by every derived class as the first basic step
for training an algorithm. It basically just initializes some internal
structures and set the self.trainset attribute.
Args:
trainset(:obj:`Trainset <surprise.Trainset>`) : A training
set, as returned by the :meth:`folds
<surprise.dataset.Dataset.folds>` method.
Returns:
self
"""
# Check if train method is overridden: this means the object is an old
# style algo (new algo only have fit() so self.__class__.train will be
# AlgoBase.train). If true, there are 2 possible cases:
# - algo.fit() was called. In this case algo.train() was skipped which
# is bad. We call it and skip this part next time we enter fit().
# Then return immediatly because fit() has already been called by
# AlgoBase.train() (which has been called by algo.train()).
# - algo.train() was called, which is the old way. In that case,
# the skip flag will ignore this.
# This is fairly ugly and hacky but I did not find anything better so
# far, in order to maintain backward compatibility... See
# tests/test_train2fit.py for supported cases.
if (guf(self.__class__.train) is not guf(AlgoBase.train) and
not self.skip_train):
self.train(trainset)
return
self.skip_train = False
self.trainset = trainset
# (re) Initialise baselines
self.bu = self.bi = None
return self
def predict(self, uid, iid, r_ui=None, clip=True, verbose=False):
"""Compute the rating prediction for given user and item.
The ``predict`` method converts raw ids to inner ids and then calls the
``estimate`` method which is defined in every derived class. If the
prediction is impossible (e.g. because the user and/or the item is
unkown), the prediction is set according to :meth:`default_prediction()
<surprise.prediction_algorithms.algo_base.AlgoBase.default_prediction>`.
Args:
uid: (Raw) id of the user. See :ref:`this note<raw_inner_note>`.
iid: (Raw) id of the item. See :ref:`this note<raw_inner_note>`.
r_ui(float): The true rating :math:`r_{ui}`. Optional, default is
``None``.
clip(bool): Whether to clip the estimation into the rating scale.
For example, if :math:`\\hat{r}_{ui}` is :math:`5.5` while the
rating scale is :math:`[1, 5]`, then :math:`\\hat{r}_{ui}` is
set to :math:`5`. Same goes if :math:`\\hat{r}_{ui} < 1`.
Default is ``True``.
verbose(bool): Whether to print details of the prediction. Default
is False.
Returns:
A :obj:`Prediction\
<surprise.prediction_algorithms.predictions.Prediction>` object
containing:
- The (raw) user id ``uid``.
- The (raw) item id ``iid``.
- The true rating ``r_ui`` (:math:`\\hat{r}_{ui}`).
- The estimated rating (:math:`\\hat{r}_{ui}`).
- Some additional details about the prediction that might be useful
for later analysis.
"""
# Convert raw ids to inner ids
try:
iuid = self.trainset.to_inner_uid(uid)
except ValueError:
iuid = 'UKN__' + str(uid)
try:
iiid = self.trainset.to_inner_iid(iid)
except ValueError:
iiid = 'UKN__' + str(iid)
details = {}
try:
est = self.estimate(iuid, iiid)
# If the details dict was also returned
if isinstance(est, tuple):
est, details = est
details['was_impossible'] = False
except PredictionImpossible as e:
est = self.default_prediction()
details['was_impossible'] = True
details['reason'] = str(e)
# Remap the rating into its initial rating scale (because the rating
# scale was translated so that ratings are all >= 1)
est -= self.trainset.offset
# clip estimate into [lower_bound, higher_bound]
if clip:
lower_bound, higher_bound = self.trainset.rating_scale
est = min(higher_bound, est)
est = max(lower_bound, est)
pred = Prediction(uid, iid, r_ui, est, details)
if verbose:
print(pred)
return pred
def default_prediction(self):
'''Used when the ``PredictionImpossible`` exception is raised during a
call to :meth:`predict()
<surprise.prediction_algorithms.algo_base.AlgoBase.predict>`. By
default, return the global mean of all ratings (can be overridden in
child classes).
Returns:
(float): The mean of all ratings in the trainset.
'''
return self.trainset.global_mean
def test(self, testset, verbose=False):
"""Test the algorithm on given testset, i.e. estimate all the ratings
in the given testset.
Args:
testset: A test set, as returned by a :ref:`cross-validation
itertor<use_cross_validation_iterators>` or by the
:meth:`build_testset() <surprise.Trainset.build_testset>`
method.
verbose(bool): Whether to print details for each predictions.
Default is False.
Returns:
A list of :class:`Prediction\
<surprise.prediction_algorithms.predictions.Prediction>` objects
that contains all the estimated ratings.
"""
# The ratings are translated back to their original scale.
predictions = [self.predict(uid,
iid,
r_ui_trans - self.trainset.offset,
verbose=verbose)
for (uid, iid, r_ui_trans) in testset]
return predictions
def compute_baselines(self):
"""Compute users and items baselines.
The way baselines are computed depends on the ``bsl_options`` parameter
passed at the creation of the algorithm (see
:ref:`baseline_estimates_configuration`).
This method is only relevant for algorithms using :func:`Pearson
baseline similarty<surprise.similarities.pearson_baseline>` or the
:class:`BaselineOnly
<surprise.prediction_algorithms.baseline_only.BaselineOnly>` algorithm.
Returns:
A tuple ``(bu, bi)``, which are users and items baselines."""
# Firt of, if this method has already been called before on the same
# trainset, then just return. Indeed, compute_baselines may be called
# more than one time, for example when a similarity metric (e.g.
# pearson_baseline) uses baseline estimates.
if self.bu is not None:
return self.bu, self.bi
method = dict(als=baseline_als,
sgd=baseline_sgd)
method_name = self.bsl_options.get('method', 'als')
try:
print('Estimating biases using', method_name + '...')
self.bu, self.bi = method[method_name](self)
return self.bu, self.bi
except KeyError:
raise ValueError('Invalid method ' + method_name +
' for baseline computation.' +
' Available methods are als and sgd.')
def compute_similarities(self):
"""Build the similarity matrix.
The way the similarity matrix is computed depends on the
``sim_options`` parameter passed at the creation of the algorithm (see
:ref:`similarity_measures_configuration`).
This method is only relevant for algorithms using a similarity measure,
such as the :ref:`k-NN algorithms <pred_package_knn_inpired>`.
Returns:
The similarity matrix."""
construction_func = {'cosine': sims.cosine,
'msd': sims.msd,
'pearson': sims.pearson,
'pearson_baseline': sims.pearson_baseline}
if self.sim_options['user_based']:
n_x, yr = self.trainset.n_users, self.trainset.ir
else:
n_x, yr = self.trainset.n_items, self.trainset.ur
min_support = self.sim_options.get('min_support', 1)
args = [n_x, yr, min_support]
name = self.sim_options.get('name', 'msd').lower()
if name == 'pearson_baseline':
shrinkage = self.sim_options.get('shrinkage', 100)
bu, bi = self.compute_baselines()
if self.sim_options['user_based']:
bx, by = bu, bi
else:
bx, by = bi, bu
args += [self.trainset.global_mean, bx, by, shrinkage]
elif name == 'cosine':
common_ratings_only = self.sim_options.get('common_ratings_only',
True)
args += [common_ratings_only]
try:
print('Computing the {0} similarity matrix...'.format(name))
sim = construction_func[name](*args)
print('Done computing similarity matrix.')
return sim
except KeyError:
raise NameError('Wrong sim name ' + name + '. Allowed values ' +
'are ' + ', '.join(construction_func.keys()) + '.')
def get_neighbors(self, iid, k):
"""Return the ``k`` nearest neighbors of ``iid``, which is the inner id
of a user or an item, depending on the ``user_based`` field of
``sim_options`` (see :ref:`similarity_measures_configuration`).
As the similarities are computed on the basis of a similarity measure,
this method is only relevant for algorithms using a similarity measure,
such as the :ref:`k-NN algorithms <pred_package_knn_inpired>`.
For a usage example, see the :ref:`FAQ <get_k_nearest_neighbors>`.
Args:
iid(int): The (inner) id of the user (or item) for which we want
the nearest neighbors. See :ref:`this note<raw_inner_note>`.
k(int): The number of neighbors to retrieve.
Returns:
The list of the ``k`` (inner) ids of the closest users (or items)
to ``iid``.
"""
if self.sim_options['user_based']:
all_instances = self.trainset.all_users
else:
all_instances = self.trainset.all_items
others = [(x, self.sim[iid, x]) for x in all_instances() if x != iid]
others.sort(key=lambda tple: tple[1], reverse=True)
k_nearest_neighbors = [j for (j, _) in others[:k]]
return k_nearest_neighbors