How To Build Your Own Prediction Algorithm - Surprise 1 Documentation
How To Build Your Own Prediction Algorithm - Surprise 1 Documentation
The basics
Want to get your hands dirty? Cool.
Crea!ng your own predic!on algorithm is pre"y simple: an algorithm is nothing but a
class derived from AlgoBase that has an estimate method. This is the method that is
called by the predict() method. It takes in an inner user id, an inner item id (see this
̂ :
note), and returns the es!mated ra!ng 𝑟𝑢𝑖
class MyOwnAlgorithm(AlgoBase):
def __init__(self):
return 3
data = Dataset.load_builtin('ml-100k')
algo = MyOwnAlgorithm()
https://surprise.readthedocs.io/en/stable/building_custom_algo.html#building-custom-algo Page 1 of 5
How to build your own prediction algorithm — Surprise 1 documentation 3/6/22, 9:35 AM
This algorithm is the dumbest we could have thought of: it just predicts a ra!ng of 3,
regardless of users and items.
If you want to store addi!onal informa!on about the predic!on, you can also return a
dic!onary with given details:
This dic!onary will be stored in the prediction as the details field and can be used
for later analysis.
method:
https://surprise.readthedocs.io/en/stable/building_custom_algo.html#building-custom-algo Page 2 of 5
How to build your own prediction algorithm — Surprise 1 documentation 3/6/22, 9:35 AM
class MyOwnAlgorithm(AlgoBase):
def __init__(self):
return self
return self.the_mean
The fit method is called e.g. by the cross_validate func!on at each fold of a cross-
valida!on process, (but you can also call it yourself). Before doing anything, you should
call the base class fit() method.
Note that the fit() method returns self . This allows to use expression like
algo.fit(trainset).test(testset) .
To illustrate its usage, let’s make an algorithm that predicts an average between the
mean of all ra!ngs, the mean ra!ng of the user and the mean ra!ng for the item:
https://surprise.readthedocs.io/en/stable/building_custom_algo.html#building-custom-algo Page 3 of 5
How to build your own prediction algorithm — Surprise 1 documentation 3/6/22, 9:35 AM
sum_means = self.trainset.global_mean
div = 1
if self.trainset.knows_user(u):
sum_means += np.mean([r for (_, r) in self.trainset.ur[u]])
div += 1
if self.trainset.knows_item(i):
sum_means += np.mean([r for (_, r) in self.trainset.ir[i]])
div += 1
Note that it would have been a be"er idea to compute all the user means in the fit
https://surprise.readthedocs.io/en/stable/building_custom_algo.html#building-custom-algo Page 4 of 5
How to build your own prediction algorithm — Surprise 1 documentation 3/6/22, 9:35 AM
class MyOwnAlgorithm(AlgoBase):
AlgoBase.__init__(self, sim_options=sim_options,
bsl_options=bsl_options)
AlgoBase.fit(self, trainset)
return self
Feel free to explore the predic!on_algorithms package source to get an idea of what
can be done.
https://surprise.readthedocs.io/en/stable/building_custom_algo.html#building-custom-algo Page 5 of 5