Train and test a recommender

Prepare the dataset as introduced before:

[1]:
import rsdiv as rs

loader = rs.MovieLens1MDownLoader()
ratings = loader.read_ratings()
items = loader.read_items()

rsdiv provides various implementations of core recommender algorithms.

To start with, a wrapper for LightFM is also supported:

[2]:
rc = rs.FMRecommender(ratings, items).fit()

30% of interactions are split for the test set by default, and the precision at the top 5 can be calculated with:

[3]:
rc.precision_at_top_k(5)
[3]:
0.15970199

the top 100 unseen recommended items for an arbitrary user, say userId: 1024, can be simply given by:

[4]:
rc.predict_top_n_item(1024, 100)
[4]:
itemId scores title genres release_date
0 2557 2.180831 I Stand Alone (Seul contre tous) [Drama] 1998
1 2865 2.078251 Sugar Town [Comedy] 1999
2 2514 2.065647 Pet Sematary II [Horror] 1992
3 2634 1.862742 Mummy [Horror] 1959
4 3542 1.828745 Coming Apart [Drama] 1969
... ... ... ... ... ...
95 3582 0.799194 Jails [Drama] 2000
96 3075 0.780045 Repulsion [Thriller] 1965
97 2943 0.779874 Indochine [Drama, Romance] 1992
98 2140 0.774891 Dark Crystal [Children's, Fantasy, Sci-Fi] 1982
99 393 0.773604 Street Fighter [Action] 1994

100 rows × 5 columns