src.fairreckitlib.model.algorithms.lenskit.lenskit_algorithms

This module contains name constants and creation wrappers for implemented lenskit algorithms.

Constants:

BIASED_MF: name of the BiasedMF algorithm.
IMPLICIT_MF: name of the ImplicitMF algorithm.
ITEM_ITEM: name of the ItemItem algorithm.
POP_SCORE: name of the PopScore algorithm.
RANDOM: name of the Random recommender.
USER_USER: name of the UserUser algorithm.

Functions:

create_biased_mf: create lenskit BiasedMF algorithm.
create_implicit_mf: create lenskit ImplicitMF algorithm.
create_item_item: create lenskit ItemItem algorithm.
create_pop_score: create lenskit PopScore algorithm.
create_random: create lenskit Random algorithm.
create_user_user: create lenskit UserUser algorithm.

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

  1"""This module contains name constants and creation wrappers for implemented lenskit algorithms.
  2
  3Constants:
  4
  5    BIASED_MF: name of the BiasedMF algorithm.
  6    IMPLICIT_MF: name of the ImplicitMF algorithm.
  7    ITEM_ITEM: name of the ItemItem algorithm.
  8    POP_SCORE: name of the PopScore algorithm.
  9    RANDOM: name of the Random recommender.
 10    USER_USER: name of the UserUser algorithm.
 11
 12Functions:
 13
 14    create_biased_mf: create lenskit BiasedMF algorithm.
 15    create_implicit_mf: create lenskit ImplicitMF algorithm.
 16    create_item_item: create lenskit ItemItem algorithm.
 17    create_pop_score: create lenskit PopScore algorithm.
 18    create_random: create lenskit Random algorithm.
 19    create_user_user: create lenskit UserUser algorithm.
 20
 21This program has been developed by students from the bachelor Computer Science at
 22Utrecht University within the Software Project course.
 23© Copyright Utrecht University (Department of Information and Computing Sciences)
 24"""
 25
 26import time
 27from typing import Any, Dict
 28
 29from lenskit.algorithms import CandidateSelector
 30from lenskit.algorithms.als import BiasedMF, ImplicitMF
 31from lenskit.algorithms.basic import PopScore, Random
 32from lenskit.algorithms.item_knn import ItemItem
 33from lenskit.algorithms.user_knn import UserUser
 34from seedbank import numpy_rng
 35
 36BIASED_MF = 'BiasedMF'
 37IMPLICIT_MF = 'ImplicitMF'
 38ITEM_ITEM = 'ItemItem'
 39POP_SCORE = 'PopScore'
 40RANDOM = 'Random'
 41USER_USER = 'UserUser'
 42
 43
 44def create_biased_mf(params: Dict[str, Any]) -> BiasedMF:
 45    """Create the lenskit BiasedMF algorithm.
 46
 47    Args:
 48        params: containing the following name-value pairs:
 49            features(int): the number of features to train.
 50            iterations(int): the number of iterations to train.
 51            user_reg(float): the regularization factor for users.
 52            item_reg(float): the regularization factor for items.
 53            damping(float): damping factor for the underlying bias.
 54            method(str): the solver to use ('cd' or 'lu').
 55            random_seed(int): the random seed or None for the current time as seed.
 56
 57    Returns:
 58        the lenskit BiasedMF algorithm.
 59    """
 60    if params['seed'] is None:
 61        params['seed'] = int(time.time())
 62
 63    return BiasedMF(
 64        params['features'],
 65        iterations=params['iterations'],
 66        reg=(params['user_reg'], params['item_reg']),
 67        damping=params['damping'],
 68        bias=True,
 69        method=params['method'],
 70        rng_spec=numpy_rng(spec=params['seed']),
 71        progress=None,
 72        save_user_features=True
 73    )
 74
 75
 76def create_implicit_mf(params: Dict[str, Any]) -> ImplicitMF:
 77    """Create the lenskit ImplicitMF algorithm.
 78
 79    Args:
 80        params: containing the following name-value pairs:
 81            features(int): the number of features to train.
 82            iterations(int): the number of iterations to train.
 83            reg(float): the regularization factor.
 84            weight(flot): the scaling weight for positive samples.
 85            use_ratings(bool): whether to use the rating column or treat
 86                every rated user-item pair as having a rating of 1.
 87            method(str): the training method ('cg' or 'lu').
 88            random_seed(int): the random seed or None for the current time as seed.
 89
 90    Returns:
 91        the lenskit ImplicitMF algorithm.
 92    """
 93    if params['seed'] is None:
 94        params['seed'] = int(time.time())
 95
 96    return ImplicitMF(
 97        params['features'],
 98        iterations=params['iterations'],
 99        reg=params['reg'],
100        weight=params['weight'],
101        use_ratings=params['use_ratings'],
102        method=params['method'],
103        rng_spec=numpy_rng(spec=params['seed']),
104        progress=None,
105        save_user_features=True
106    )
107
108
109def create_item_item(params: Dict[str, Any], feedback: str) -> ItemItem:
110    """Create the lenskit ItemItem algorithm.
111
112    Args:
113        params: containing the following name-value pairs:
114            max_neighbors(int): the maximum number of neighbors for scoring each item.
115            min_neighbors(int): the minimum number of neighbors for scoring each item.
116            min_similarity(float): minimum similarity threshold for considering a neighbor.
117        feedback: control how feedback should be interpreted ('explicit' or 'implicit').
118
119    Returns:
120        the lenskit ItemItem algorithm.
121    """
122    return ItemItem(
123        params['max_neighbors'],
124        min_nbrs=params['min_neighbors'],
125        min_sim=params['min_similarity'],
126        save_nbrs=None,
127        feedback=feedback
128    )
129
130
131def create_pop_score(params: Dict[str, Any]) -> PopScore:
132    """Create the lenskit PopScore algorithm.
133
134    Args:
135        params: containing the following name-value pairs:
136            score_method(str): for computing popularity scores ('quantile', 'rank' or 'count').
137
138    Returns:
139        the lenskit PopScore algorithm.
140    """
141    return PopScore(
142        score_method=params['score_method']
143    )
144
145
146def create_random(params: Dict[str, Any], selector: CandidateSelector) -> Random:
147    """Create the lenskit Random algorithm.
148
149    Args:
150        params: containing the following name-value pairs:
151            random_seed(int): the random seed or None for the current time as seed.
152        selector: that selects candidate items for recommendations.
153
154    Returns:
155        the lenskit Random algorithm.
156    """
157    if params['seed'] is None:
158        params['seed'] = int(time.time())
159
160    return Random(
161        selector=selector,
162        rng_spec=numpy_rng(spec=params['seed'])
163    )
164
165
166def create_user_user(params: Dict[str, Any], feedback: str) -> UserUser:
167    """Create the lenskit UserUser algorithm.
168
169    Args:
170        params: containing the following name-value pairs:
171            max_neighbors(int): the maximum number of neighbors for scoring each item.
172            min_neighbors(int): the minimum number of neighbors for scoring each item.
173            min_similarity(float): minimum similarity threshold for considering a neighbor.
174        feedback: control how feedback should be interpreted ('explicit' or 'implicit').
175
176    Returns:
177        the lenskit UserUser algorithm.
178    """
179    return UserUser(
180        params['max_neighbors'],
181        min_nbrs=params['min_neighbors'],
182        min_sim=params['min_similarity'],
183        feedback=feedback
184    )
def create_biased_mf(params: Dict[str, Any]) -> lenskit.algorithms.als.BiasedMF:
45def create_biased_mf(params: Dict[str, Any]) -> BiasedMF:
46    """Create the lenskit BiasedMF algorithm.
47
48    Args:
49        params: containing the following name-value pairs:
50            features(int): the number of features to train.
51            iterations(int): the number of iterations to train.
52            user_reg(float): the regularization factor for users.
53            item_reg(float): the regularization factor for items.
54            damping(float): damping factor for the underlying bias.
55            method(str): the solver to use ('cd' or 'lu').
56            random_seed(int): the random seed or None for the current time as seed.
57
58    Returns:
59        the lenskit BiasedMF algorithm.
60    """
61    if params['seed'] is None:
62        params['seed'] = int(time.time())
63
64    return BiasedMF(
65        params['features'],
66        iterations=params['iterations'],
67        reg=(params['user_reg'], params['item_reg']),
68        damping=params['damping'],
69        bias=True,
70        method=params['method'],
71        rng_spec=numpy_rng(spec=params['seed']),
72        progress=None,
73        save_user_features=True
74    )

Create the lenskit BiasedMF algorithm.

Args: params: containing the following name-value pairs: features(int): the number of features to train. iterations(int): the number of iterations to train. user_reg(float): the regularization factor for users. item_reg(float): the regularization factor for items. damping(float): damping factor for the underlying bias. method(str): the solver to use ('cd' or 'lu'). random_seed(int): the random seed or None for the current time as seed.

Returns: the lenskit BiasedMF algorithm.

def create_implicit_mf(params: Dict[str, Any]) -> lenskit.algorithms.als.ImplicitMF:
 77def create_implicit_mf(params: Dict[str, Any]) -> ImplicitMF:
 78    """Create the lenskit ImplicitMF algorithm.
 79
 80    Args:
 81        params: containing the following name-value pairs:
 82            features(int): the number of features to train.
 83            iterations(int): the number of iterations to train.
 84            reg(float): the regularization factor.
 85            weight(flot): the scaling weight for positive samples.
 86            use_ratings(bool): whether to use the rating column or treat
 87                every rated user-item pair as having a rating of 1.
 88            method(str): the training method ('cg' or 'lu').
 89            random_seed(int): the random seed or None for the current time as seed.
 90
 91    Returns:
 92        the lenskit ImplicitMF algorithm.
 93    """
 94    if params['seed'] is None:
 95        params['seed'] = int(time.time())
 96
 97    return ImplicitMF(
 98        params['features'],
 99        iterations=params['iterations'],
100        reg=params['reg'],
101        weight=params['weight'],
102        use_ratings=params['use_ratings'],
103        method=params['method'],
104        rng_spec=numpy_rng(spec=params['seed']),
105        progress=None,
106        save_user_features=True
107    )

Create the lenskit ImplicitMF algorithm.

Args: params: containing the following name-value pairs: features(int): the number of features to train. iterations(int): the number of iterations to train. reg(float): the regularization factor. weight(flot): the scaling weight for positive samples. use_ratings(bool): whether to use the rating column or treat every rated user-item pair as having a rating of 1. method(str): the training method ('cg' or 'lu'). random_seed(int): the random seed or None for the current time as seed.

Returns: the lenskit ImplicitMF algorithm.

def create_item_item( params: Dict[str, Any], feedback: str) -> lenskit.algorithms.item_knn.ItemItem:
110def create_item_item(params: Dict[str, Any], feedback: str) -> ItemItem:
111    """Create the lenskit ItemItem algorithm.
112
113    Args:
114        params: containing the following name-value pairs:
115            max_neighbors(int): the maximum number of neighbors for scoring each item.
116            min_neighbors(int): the minimum number of neighbors for scoring each item.
117            min_similarity(float): minimum similarity threshold for considering a neighbor.
118        feedback: control how feedback should be interpreted ('explicit' or 'implicit').
119
120    Returns:
121        the lenskit ItemItem algorithm.
122    """
123    return ItemItem(
124        params['max_neighbors'],
125        min_nbrs=params['min_neighbors'],
126        min_sim=params['min_similarity'],
127        save_nbrs=None,
128        feedback=feedback
129    )

Create the lenskit ItemItem algorithm.

Args: params: containing the following name-value pairs: max_neighbors(int): the maximum number of neighbors for scoring each item. min_neighbors(int): the minimum number of neighbors for scoring each item. min_similarity(float): minimum similarity threshold for considering a neighbor. feedback: control how feedback should be interpreted ('explicit' or 'implicit').

Returns: the lenskit ItemItem algorithm.

def create_pop_score(params: Dict[str, Any]) -> lenskit.algorithms.basic.PopScore:
132def create_pop_score(params: Dict[str, Any]) -> PopScore:
133    """Create the lenskit PopScore algorithm.
134
135    Args:
136        params: containing the following name-value pairs:
137            score_method(str): for computing popularity scores ('quantile', 'rank' or 'count').
138
139    Returns:
140        the lenskit PopScore algorithm.
141    """
142    return PopScore(
143        score_method=params['score_method']
144    )

Create the lenskit PopScore algorithm.

Args: params: containing the following name-value pairs: score_method(str): for computing popularity scores ('quantile', 'rank' or 'count').

Returns: the lenskit PopScore algorithm.

def create_random( params: Dict[str, Any], selector: lenskit.algorithms.CandidateSelector) -> lenskit.algorithms.basic.Random:
147def create_random(params: Dict[str, Any], selector: CandidateSelector) -> Random:
148    """Create the lenskit Random algorithm.
149
150    Args:
151        params: containing the following name-value pairs:
152            random_seed(int): the random seed or None for the current time as seed.
153        selector: that selects candidate items for recommendations.
154
155    Returns:
156        the lenskit Random algorithm.
157    """
158    if params['seed'] is None:
159        params['seed'] = int(time.time())
160
161    return Random(
162        selector=selector,
163        rng_spec=numpy_rng(spec=params['seed'])
164    )

Create the lenskit Random algorithm.

Args: params: containing the following name-value pairs: random_seed(int): the random seed or None for the current time as seed. selector: that selects candidate items for recommendations.

Returns: the lenskit Random algorithm.

def create_user_user( params: Dict[str, Any], feedback: str) -> lenskit.algorithms.user_knn.UserUser:
167def create_user_user(params: Dict[str, Any], feedback: str) -> UserUser:
168    """Create the lenskit UserUser algorithm.
169
170    Args:
171        params: containing the following name-value pairs:
172            max_neighbors(int): the maximum number of neighbors for scoring each item.
173            min_neighbors(int): the minimum number of neighbors for scoring each item.
174            min_similarity(float): minimum similarity threshold for considering a neighbor.
175        feedback: control how feedback should be interpreted ('explicit' or 'implicit').
176
177    Returns:
178        the lenskit UserUser algorithm.
179    """
180    return UserUser(
181        params['max_neighbors'],
182        min_nbrs=params['min_neighbors'],
183        min_sim=params['min_similarity'],
184        feedback=feedback
185    )

Create the lenskit UserUser algorithm.

Args: params: containing the following name-value pairs: max_neighbors(int): the maximum number of neighbors for scoring each item. min_neighbors(int): the minimum number of neighbors for scoring each item. min_similarity(float): minimum similarity threshold for considering a neighbor. feedback: control how feedback should be interpreted ('explicit' or 'implicit').

Returns: the lenskit UserUser algorithm.