src.fairreckitlib.model.algorithms.elliot.elliot_recommender

This module contains the elliot recommender and creation functions.

Classes:

ElliotRecommender: recommender implementation for elliot.

Functions:

create_funk_svd: create FunkSVD recommender (factory creation compatible).
create_item_knn: create ItemKNN recommender (factory creation compatible).
create_most_pop: create MostPop recommender (factory creation compatible).
create_multi_vae: create MultiVAE recommender (factory creation compatible).
create_pure_svd: create PureSVD recommender (factory creation compatible).
create_random: create Random recommender (factory creation compatible).
create_user_knn: create UserKNN recommender (factory creation compatible).

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 the elliot recommender and creation functions.
  2
  3Classes:
  4
  5    ElliotRecommender: recommender implementation for elliot.
  6
  7Functions:
  8
  9    create_funk_svd: create FunkSVD recommender (factory creation compatible).
 10    create_item_knn: create ItemKNN recommender (factory creation compatible).
 11    create_most_pop: create MostPop recommender (factory creation compatible).
 12    create_multi_vae: create MultiVAE recommender (factory creation compatible).
 13    create_pure_svd: create PureSVD recommender (factory creation compatible).
 14    create_random: create Random recommender (factory creation compatible).
 15    create_user_knn: create UserKNN recommender (factory creation compatible).
 16
 17This program has been developed by students from the bachelor Computer Science at
 18Utrecht University within the Software Project course.
 19© Copyright Utrecht University (Department of Information and Computing Sciences)
 20"""
 21
 22import time
 23from typing import Any, Dict, List
 24
 25import pandas as pd
 26
 27from ..base_recommender import Recommender
 28
 29
 30class ElliotRecommender(Recommender):
 31    """Recommender implementation for the Elliot framework."""
 32
 33    def __init__(self, name: str, params: Dict[str, Any], **kwargs):
 34        """Construct the elliot recommender.
 35
 36        The recommender is not procedural, instead it serves as a wrapper
 37        that holds the correct parameters used by the framework which are
 38        used in the model pipeline.
 39
 40        Args:
 41            name: the name of the recommender.
 42            params: the parameters of the recommender.
 43
 44        Keyword Args:
 45            num_threads(int): the max number of threads the recommender can use.
 46            rated_items_filter(bool): whether to filter already rated items when
 47                producing item recommendations.
 48        """
 49        Recommender.__init__(self, name, params, kwargs['num_threads'],
 50                             kwargs['rated_items_filter'])
 51        if not self.rated_items_filter:
 52            raise RuntimeError('no rated items filter is not supported.')
 53
 54    def on_train(self, train_set: Any) -> None:
 55        """Train the elliot model not supported."""
 56        raise RuntimeError('training is done by running the framework')
 57
 58    def on_recommend(self, user: int, num_items: int) -> pd.DataFrame:
 59        """Recommend with the elliot model not supported."""
 60        raise RuntimeError('recommending is done by running the framework')
 61
 62    def on_recommend_batch(self, users: List[int], num_items: int) -> pd.DataFrame:
 63        """Recommend batching with the elliot model not supported."""
 64        raise RuntimeError('recommending is done by running the framework')
 65
 66
 67def create_funk_svd(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
 68    """Create the FunkSVD recommender.
 69
 70    Args:
 71        name: the name of the algorithm.
 72        params: containing the following name-value pairs:
 73            epochs(int): number of iterations.
 74            factors(int): number of factors of feature embeddings.
 75            learning_rate(float): the learning rate.
 76            regularization_factors(float): regularization coefficient for latent factors.
 77            regularization_bias(flot): regularization coefficient for bias.
 78            seed(int): the random seed or None for the current time as seed.
 79
 80    Returns:
 81        the ElliotRecommender wrapper of FunkSVD.
 82    """
 83    elliot_params = {
 84        'epochs': params['iterations'],
 85        'batch_size': 512,
 86        'factors': params['factors'],
 87        'lr': params['learning_rate'],
 88        'reg_w': params['regularization_factors'],
 89        'reg_b': params['regularization_bias'],
 90        'seed': params['seed'] if params.get('seed') else int(time.time())
 91    }
 92
 93    return ElliotRecommender(name, elliot_params, **kwargs)
 94
 95
 96def create_item_knn(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
 97    """Create the ItemKNN recommender.
 98
 99    Args:
100        name: the name of the algorithm.
101        params: containing the following name-value pairs:
102            neighbors(int): number of item neighbors.
103            similarity(str): similarity function to use.
104            implementation(str): implementation type (‘aiolli’ or ‘classical’).
105
106    Returns:
107        the ElliotRecommender wrapper of ItemKNN.
108    """
109    elliot_params = {
110        'neighbors': params['neighbors'],
111        'similarity': params['similarity'],
112        'implementation': params['implementation'],
113    }
114
115    return ElliotRecommender(name, elliot_params, **kwargs)
116
117
118def create_most_pop(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
119    """Create the MostPop recommender.
120
121    Args:
122        name: the name of the algorithm.
123        params: there are no parameters for this algorithm.
124
125    Returns:
126        the ElliotRecommender wrapper of MostPop.
127    """
128    return ElliotRecommender(name, params, **kwargs)
129
130
131def create_multi_vae(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
132    """Create the MultiVAE recommender.
133
134    Args:
135        name: the name of the algorithm.
136        params: containing the following name-value pairs:
137            iterations(int): number of iterations.
138            factors(int): number of latent factors.
139            learning_rate(float): the learning rate.
140            intermediate_dimensions(int): number of intermediate dimension.
141            regularization_factors(float): regularization coefficient.
142            dropout_probability(float): the dropout probability.
143            seed(int): the random seed or None for the current time as seed.
144
145    Returns:
146        the ElliotRecommender wrapper of MultiVAE.
147    """
148    elliot_params = {
149        'epochs': params['iterations'],
150        'batch_size': 512,
151        'lr': params['learning_rate'],
152        'reg_lambda': params['regularization_factors'],
153        'intermediate_dim': params['intermediate_dimensions'],
154        'latent_dim': params['factors'],
155        'dropout_pkeep': params['dropout_probability'],
156        'seed': params['seed'] if params.get('seed') else int(time.time())
157    }
158
159    return ElliotRecommender(name, elliot_params, **kwargs)
160
161
162def create_pure_svd(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
163    """Create the PureSVD recommender.
164
165    Args:
166        name: the name of the algorithm.
167        params: containing the following name-value pairs:
168            factors(int): number of latent factors.
169            seed(int): the random seed or None for the current time as seed.
170
171    Returns:
172        the ElliotRecommender wrapper of PureSVD.
173    """
174    elliot_params = {
175        'factors': params['factors'],
176        'seed': params['seed'] if params.get('seed') else int(time.time())
177    }
178
179    return ElliotRecommender(name, elliot_params, **kwargs)
180
181
182def create_random(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
183    """Create the Random recommender.
184
185    Args:
186        name: the name of the algorithm.
187        params: containing the following name-value pairs:
188            seed(int): the random seed or None for the current time as seed.
189
190    Returns:
191        the ElliotRecommender wrapper of Random.
192    """
193    elliot_params = {
194        'random_seed': params['seed'] if params.get('seed') else int(time.time())
195    }
196
197    return ElliotRecommender(name, elliot_params, **kwargs)
198
199
200def create_svd_pp(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
201    """Create the SVDpp recommender.
202
203    Args:
204        name: the name of the algorithm.
205        params: containing the following name-value pairs:
206            iterations(int): number of iterations.
207            factors(int): number of latent factors.
208            learning_rate(float): the learning rate.
209            regularization_factors(float): regularization coefficient for latent factors.
210            regularization_bias(float): regularization coefficient for bias.
211            seed(int): the random seed or None for the current time as seed.
212
213    Returns:
214        the ElliotRecommender wrapper of SVDpp.
215    """
216    elliot_params = {
217        'epochs': params['iterations'],
218        'batch_size': 512,
219        'factors': params['factors'],
220        'lr': params['learning_rate'],
221        'reg_w': params['regularization_factors'],
222        'reg_b': params['regularization_bias'],
223        'seed': params['seed'] if params.get('seed') else int(time.time())
224    }
225
226    return ElliotRecommender(name, elliot_params, **kwargs)
227
228
229def create_user_knn(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
230    """Create the UserKNN recommender.
231
232    Args:
233        name: the name of the algorithm.
234        params: containing the following name-value pairs:
235            neighbors(int): number of user neighbors.
236            similarity(str): similarity function to use.
237            implementation(str): implementation type (‘aiolli’ or ‘classical’).
238
239    Returns:
240        the ElliotRecommender wrapper of UserKNN.
241    """
242    elliot_params = {
243        'neighbors': params['neighbors'],
244        'similarity': params['similarity'],
245        'implementation': params['implementation'],
246    }
247
248    return ElliotRecommender(name, elliot_params, **kwargs)
31class ElliotRecommender(Recommender):
32    """Recommender implementation for the Elliot framework."""
33
34    def __init__(self, name: str, params: Dict[str, Any], **kwargs):
35        """Construct the elliot recommender.
36
37        The recommender is not procedural, instead it serves as a wrapper
38        that holds the correct parameters used by the framework which are
39        used in the model pipeline.
40
41        Args:
42            name: the name of the recommender.
43            params: the parameters of the recommender.
44
45        Keyword Args:
46            num_threads(int): the max number of threads the recommender can use.
47            rated_items_filter(bool): whether to filter already rated items when
48                producing item recommendations.
49        """
50        Recommender.__init__(self, name, params, kwargs['num_threads'],
51                             kwargs['rated_items_filter'])
52        if not self.rated_items_filter:
53            raise RuntimeError('no rated items filter is not supported.')
54
55    def on_train(self, train_set: Any) -> None:
56        """Train the elliot model not supported."""
57        raise RuntimeError('training is done by running the framework')
58
59    def on_recommend(self, user: int, num_items: int) -> pd.DataFrame:
60        """Recommend with the elliot model not supported."""
61        raise RuntimeError('recommending is done by running the framework')
62
63    def on_recommend_batch(self, users: List[int], num_items: int) -> pd.DataFrame:
64        """Recommend batching with the elliot model not supported."""
65        raise RuntimeError('recommending is done by running the framework')

Recommender implementation for the Elliot framework.

ElliotRecommender(name: str, params: Dict[str, Any], **kwargs)
34    def __init__(self, name: str, params: Dict[str, Any], **kwargs):
35        """Construct the elliot recommender.
36
37        The recommender is not procedural, instead it serves as a wrapper
38        that holds the correct parameters used by the framework which are
39        used in the model pipeline.
40
41        Args:
42            name: the name of the recommender.
43            params: the parameters of the recommender.
44
45        Keyword Args:
46            num_threads(int): the max number of threads the recommender can use.
47            rated_items_filter(bool): whether to filter already rated items when
48                producing item recommendations.
49        """
50        Recommender.__init__(self, name, params, kwargs['num_threads'],
51                             kwargs['rated_items_filter'])
52        if not self.rated_items_filter:
53            raise RuntimeError('no rated items filter is not supported.')

Construct the elliot recommender.

The recommender is not procedural, instead it serves as a wrapper that holds the correct parameters used by the framework which are used in the model pipeline.

Args: name: the name of the recommender. params: the parameters of the recommender.

Keyword Args: num_threads(int): the max number of threads the recommender can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations.

def on_train(self, train_set: Any) -> None:
55    def on_train(self, train_set: Any) -> None:
56        """Train the elliot model not supported."""
57        raise RuntimeError('training is done by running the framework')

Train the elliot model not supported.

def on_recommend(self, user: int, num_items: int) -> pandas.core.frame.DataFrame:
59    def on_recommend(self, user: int, num_items: int) -> pd.DataFrame:
60        """Recommend with the elliot model not supported."""
61        raise RuntimeError('recommending is done by running the framework')

Recommend with the elliot model not supported.

def on_recommend_batch(self, users: List[int], num_items: int) -> pandas.core.frame.DataFrame:
63    def on_recommend_batch(self, users: List[int], num_items: int) -> pd.DataFrame:
64        """Recommend batching with the elliot model not supported."""
65        raise RuntimeError('recommending is done by running the framework')

Recommend batching with the elliot model not supported.

def create_funk_svd( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
68def create_funk_svd(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
69    """Create the FunkSVD recommender.
70
71    Args:
72        name: the name of the algorithm.
73        params: containing the following name-value pairs:
74            epochs(int): number of iterations.
75            factors(int): number of factors of feature embeddings.
76            learning_rate(float): the learning rate.
77            regularization_factors(float): regularization coefficient for latent factors.
78            regularization_bias(flot): regularization coefficient for bias.
79            seed(int): the random seed or None for the current time as seed.
80
81    Returns:
82        the ElliotRecommender wrapper of FunkSVD.
83    """
84    elliot_params = {
85        'epochs': params['iterations'],
86        'batch_size': 512,
87        'factors': params['factors'],
88        'lr': params['learning_rate'],
89        'reg_w': params['regularization_factors'],
90        'reg_b': params['regularization_bias'],
91        'seed': params['seed'] if params.get('seed') else int(time.time())
92    }
93
94    return ElliotRecommender(name, elliot_params, **kwargs)

Create the FunkSVD recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: epochs(int): number of iterations. factors(int): number of factors of feature embeddings. learning_rate(float): the learning rate. regularization_factors(float): regularization coefficient for latent factors. regularization_bias(flot): regularization coefficient for bias. seed(int): the random seed or None for the current time as seed.

Returns: the ElliotRecommender wrapper of FunkSVD.

def create_item_knn( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
 97def create_item_knn(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
 98    """Create the ItemKNN recommender.
 99
100    Args:
101        name: the name of the algorithm.
102        params: containing the following name-value pairs:
103            neighbors(int): number of item neighbors.
104            similarity(str): similarity function to use.
105            implementation(str): implementation type (‘aiolli’ or ‘classical’).
106
107    Returns:
108        the ElliotRecommender wrapper of ItemKNN.
109    """
110    elliot_params = {
111        'neighbors': params['neighbors'],
112        'similarity': params['similarity'],
113        'implementation': params['implementation'],
114    }
115
116    return ElliotRecommender(name, elliot_params, **kwargs)

Create the ItemKNN recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: neighbors(int): number of item neighbors. similarity(str): similarity function to use. implementation(str): implementation type (‘aiolli’ or ‘classical’).

Returns: the ElliotRecommender wrapper of ItemKNN.

def create_most_pop( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
119def create_most_pop(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
120    """Create the MostPop recommender.
121
122    Args:
123        name: the name of the algorithm.
124        params: there are no parameters for this algorithm.
125
126    Returns:
127        the ElliotRecommender wrapper of MostPop.
128    """
129    return ElliotRecommender(name, params, **kwargs)

Create the MostPop recommender.

Args: name: the name of the algorithm. params: there are no parameters for this algorithm.

Returns: the ElliotRecommender wrapper of MostPop.

def create_multi_vae( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
132def create_multi_vae(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
133    """Create the MultiVAE recommender.
134
135    Args:
136        name: the name of the algorithm.
137        params: containing the following name-value pairs:
138            iterations(int): number of iterations.
139            factors(int): number of latent factors.
140            learning_rate(float): the learning rate.
141            intermediate_dimensions(int): number of intermediate dimension.
142            regularization_factors(float): regularization coefficient.
143            dropout_probability(float): the dropout probability.
144            seed(int): the random seed or None for the current time as seed.
145
146    Returns:
147        the ElliotRecommender wrapper of MultiVAE.
148    """
149    elliot_params = {
150        'epochs': params['iterations'],
151        'batch_size': 512,
152        'lr': params['learning_rate'],
153        'reg_lambda': params['regularization_factors'],
154        'intermediate_dim': params['intermediate_dimensions'],
155        'latent_dim': params['factors'],
156        'dropout_pkeep': params['dropout_probability'],
157        'seed': params['seed'] if params.get('seed') else int(time.time())
158    }
159
160    return ElliotRecommender(name, elliot_params, **kwargs)

Create the MultiVAE recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: iterations(int): number of iterations. factors(int): number of latent factors. learning_rate(float): the learning rate. intermediate_dimensions(int): number of intermediate dimension. regularization_factors(float): regularization coefficient. dropout_probability(float): the dropout probability. seed(int): the random seed or None for the current time as seed.

Returns: the ElliotRecommender wrapper of MultiVAE.

def create_pure_svd( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
163def create_pure_svd(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
164    """Create the PureSVD recommender.
165
166    Args:
167        name: the name of the algorithm.
168        params: containing the following name-value pairs:
169            factors(int): number of latent factors.
170            seed(int): the random seed or None for the current time as seed.
171
172    Returns:
173        the ElliotRecommender wrapper of PureSVD.
174    """
175    elliot_params = {
176        'factors': params['factors'],
177        'seed': params['seed'] if params.get('seed') else int(time.time())
178    }
179
180    return ElliotRecommender(name, elliot_params, **kwargs)

Create the PureSVD recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: factors(int): number of latent factors. seed(int): the random seed or None for the current time as seed.

Returns: the ElliotRecommender wrapper of PureSVD.

def create_random( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
183def create_random(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
184    """Create the Random recommender.
185
186    Args:
187        name: the name of the algorithm.
188        params: containing the following name-value pairs:
189            seed(int): the random seed or None for the current time as seed.
190
191    Returns:
192        the ElliotRecommender wrapper of Random.
193    """
194    elliot_params = {
195        'random_seed': params['seed'] if params.get('seed') else int(time.time())
196    }
197
198    return ElliotRecommender(name, elliot_params, **kwargs)

Create the Random recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: seed(int): the random seed or None for the current time as seed.

Returns: the ElliotRecommender wrapper of Random.

def create_svd_pp( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
201def create_svd_pp(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
202    """Create the SVDpp recommender.
203
204    Args:
205        name: the name of the algorithm.
206        params: containing the following name-value pairs:
207            iterations(int): number of iterations.
208            factors(int): number of latent factors.
209            learning_rate(float): the learning rate.
210            regularization_factors(float): regularization coefficient for latent factors.
211            regularization_bias(float): regularization coefficient for bias.
212            seed(int): the random seed or None for the current time as seed.
213
214    Returns:
215        the ElliotRecommender wrapper of SVDpp.
216    """
217    elliot_params = {
218        'epochs': params['iterations'],
219        'batch_size': 512,
220        'factors': params['factors'],
221        'lr': params['learning_rate'],
222        'reg_w': params['regularization_factors'],
223        'reg_b': params['regularization_bias'],
224        'seed': params['seed'] if params.get('seed') else int(time.time())
225    }
226
227    return ElliotRecommender(name, elliot_params, **kwargs)

Create the SVDpp recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: iterations(int): number of iterations. factors(int): number of latent factors. learning_rate(float): the learning rate. regularization_factors(float): regularization coefficient for latent factors. regularization_bias(float): regularization coefficient for bias. seed(int): the random seed or None for the current time as seed.

Returns: the ElliotRecommender wrapper of SVDpp.

def create_user_knn( name: str, params: Dict[str, Any], **kwargs) -> src.fairreckitlib.model.algorithms.elliot.elliot_recommender.ElliotRecommender:
230def create_user_knn(name: str, params: Dict[str, Any], **kwargs) -> ElliotRecommender:
231    """Create the UserKNN recommender.
232
233    Args:
234        name: the name of the algorithm.
235        params: containing the following name-value pairs:
236            neighbors(int): number of user neighbors.
237            similarity(str): similarity function to use.
238            implementation(str): implementation type (‘aiolli’ or ‘classical’).
239
240    Returns:
241        the ElliotRecommender wrapper of UserKNN.
242    """
243    elliot_params = {
244        'neighbors': params['neighbors'],
245        'similarity': params['similarity'],
246        'implementation': params['implementation'],
247    }
248
249    return ElliotRecommender(name, elliot_params, **kwargs)

Create the UserKNN recommender.

Args: name: the name of the algorithm. params: containing the following name-value pairs: neighbors(int): number of user neighbors. similarity(str): similarity function to use. implementation(str): implementation type (‘aiolli’ or ‘classical’).

Returns: the ElliotRecommender wrapper of UserKNN.