src.fairreckitlib.model.algorithms.lenskit.lenskit_recommender
This module contains the lenskit recommender and creation functions.
Classes:
LensKitRecommender: recommender implementation for lenskit.
Functions:
create_candidate_selector: create CandidateSelector for a rated items filter.
create_biased_mf: create BiasedMF recommender (factory creation compatible).
create_implicit_mf: create ImplicitMF recommender (factory creation compatible).
create_item_item: create ItemItem recommender (factory creation compatible).
create_pop_score: create PopScore recommender (factory creation compatible).
create_random: create Random recommender (factory creation compatible).
create_user_user: create UserUser 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 lenskit recommender and creation functions. 2 3Classes: 4 5 LensKitRecommender: recommender implementation for lenskit. 6 7Functions: 8 9 create_candidate_selector: create CandidateSelector for a rated items filter. 10 create_biased_mf: create BiasedMF recommender (factory creation compatible). 11 create_implicit_mf: create ImplicitMF recommender (factory creation compatible). 12 create_item_item: create ItemItem recommender (factory creation compatible). 13 create_pop_score: create PopScore recommender (factory creation compatible). 14 create_random: create Random recommender (factory creation compatible). 15 create_user_user: create UserUser 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 22from typing import Any, Dict, List 23 24import numpy as np 25import lenskit 26from lenskit import batch 27from lenskit.algorithms import CandidateSelector 28from lenskit.algorithms.basic import AllItemsCandidateSelector, UnratedItemCandidateSelector 29from lenskit.algorithms.ranking import TopN 30import pandas as pd 31 32from ..base_recommender import Recommender 33from . import lenskit_algorithms 34 35 36class LensKitRecommender(Recommender): 37 """Recommender implementation for the LensKit framework.""" 38 39 def __init__(self, algo: lenskit.Recommender, name: str, params: Dict[str, Any], **kwargs): 40 """Construct the lenskit recommender. 41 42 Args: 43 algo: the lenskit recommender algorithm. 44 name: the name of the recommender. 45 params: the parameters of the recommender. 46 47 Keyword Args: 48 num_threads(int): the max number of threads the recommender can use. 49 rated_items_filter(bool): whether to filter already rated items when 50 producing item recommendations. 51 """ 52 Recommender.__init__(self, name, params, 53 kwargs['num_threads'], kwargs['rated_items_filter']) 54 self.algo = algo 55 56 def on_train(self, train_set: pd.DataFrame) -> None: 57 """Fit the lenskit algorithm on the train set. 58 59 The recommender should be trained with a dataframe matrix. 60 61 Args: 62 train_set: the set to train the recommender with. 63 64 Raises: 65 ArithmeticError: possibly raised by an algorithm on training. 66 MemoryError: possibly raised by an algorithm on training. 67 RuntimeError: possibly raised by an algorithm on training. 68 TypeError: when the train set is not a pandas dataframe. 69 """ 70 if not isinstance(train_set, pd.DataFrame): 71 raise TypeError('Expected recommender to be trained with a dataframe matrix') 72 73 self.algo.fit(train_set) 74 75 def on_recommend(self, user: int, num_items: int) -> pd.DataFrame: 76 """Compute item recommendations for the specified user. 77 78 Lenskit recommenders have an implementation that is exactly the 79 same as the required interface. 80 81 Args: 82 user: the user ID to compute recommendations for. 83 num_items: the number of item recommendations to produce. 84 85 Raises: 86 ArithmeticError: possibly raised by a recommender on testing. 87 MemoryError: possibly raised by a recommender on testing. 88 RuntimeError: when the recommender is not trained yet. 89 90 Returns: 91 dataframe with the columns: 'item' and 'score'. 92 """ 93 recs = self.algo.recommend(user, n=num_items) 94 # random algo does not produce a score 95 if self.get_name() == lenskit_algorithms.RANDOM: 96 recs['score'] = np.full(num_items, 1) 97 98 return recs 99 100 def on_recommend_batch(self, users: List[int], num_items: int) -> pd.DataFrame: 101 """Compute the items recommendations for each of the specified users. 102 103 Lenskit recommenders have a batch implementation available that allows for 104 recommending items using multiple 'jobs'. 105 106 Args: 107 users: the user ID's to compute recommendations for. 108 num_items: the number of item recommendations to produce. 109 110 Raises: 111 ArithmeticError: possibly raised by a recommender on testing. 112 MemoryError: possibly raised by a recommender on testing. 113 RuntimeError: when the recommender is not trained yet. 114 115 Returns: 116 dataframe with the columns: 'rank', 'user', 'item', 'score'. 117 """ 118 n_jobs = self.num_threads if self.num_threads > 0 else None 119 recs = batch.recommend(self.algo, users, num_items, n_jobs=n_jobs) 120 121 # random algo does not produce a score 122 if self.get_name() == lenskit_algorithms.RANDOM: 123 recs['score'] = np.full(len(users) * num_items, 1) 124 125 return recs[['rank', 'user', 'item', 'score']] 126 127 128def create_candidate_selector(rated_items_filter: bool) -> CandidateSelector: 129 """Create a candidate selector for the specified filter. 130 131 Args: 132 rated_items_filter(bool): whether to filter already rated items when 133 producing item recommendations. 134 135 Returns: 136 the corresponding lenskit candidate selector. 137 """ 138 return UnratedItemCandidateSelector() if rated_items_filter else AllItemsCandidateSelector() 139 140 141def create_biased_mf(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 142 """Create the BiasedMF recommender. 143 144 Args: 145 name: the name of the algorithm. 146 params: containing the following name-value pairs: 147 features(int): the number of features to train. 148 iterations(int): the number of iterations to train. 149 user_reg(float): the regularization factor for users. 150 item_reg(float): the regularization factor for items. 151 damping(float): damping factor for the underlying bias. 152 method(str): the solver to use ('cd' or 'lu'). 153 random_seed(int): the random seed or None for the current time as seed. 154 155 Keyword Args: 156 num_threads(int): the max number of threads the algorithm can use. 157 rated_items_filter(bool): whether to filter already rated items when 158 producing item recommendations. 159 160 Returns: 161 the LensKitRecommender wrapper of BiasedMF. 162 """ 163 algo = TopN( 164 lenskit_algorithms.create_biased_mf(params), 165 create_candidate_selector(kwargs['rated_items_filter']) 166 ) 167 168 return LensKitRecommender(algo, name, params, **kwargs) 169 170 171def create_implicit_mf(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 172 """Create the ImplicitMF recommender. 173 174 Args: 175 name: the name of the algorithm. 176 params: containing the following name-value pairs: 177 features(int): the number of features to train. 178 iterations(int): the number of iterations to train. 179 reg(float): the regularization factor. 180 weight(flot): the scaling weight for positive samples. 181 use_ratings(bool): whether to use the rating column or treat 182 every rated user-item pair as having a rating of 1. 183 method(str): the training method ('cg' or 'lu'). 184 random_seed(int): the random seed or None for the current time as seed. 185 186 Keyword Args: 187 num_threads(int): the max number of threads the algorithm can use. 188 rated_items_filter(bool): whether to filter already rated items when 189 producing item recommendations. 190 191 Returns: 192 the LensKitRecommender wrapper of ImplicitMF. 193 """ 194 algo = TopN( 195 lenskit_algorithms.create_implicit_mf(params), 196 create_candidate_selector(kwargs['rated_items_filter']) 197 ) 198 199 return LensKitRecommender(algo, name, params, **kwargs) 200 201 202def create_item_item(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 203 """Create the ItemItem recommender. 204 205 Args: 206 name: the name of the algorithm. 207 params: containing the following name-value pairs: 208 max_neighbors(int): the maximum number of neighbors for scoring each item. 209 min_neighbors(int): the minimum number of neighbors for scoring each item. 210 min_similarity(float): minimum similarity threshold for considering a neighbor. 211 212 Keyword Args: 213 num_threads(int): the max number of threads the algorithm can use. 214 rated_items_filter(bool): whether to filter already rated items when 215 producing item recommendations. 216 rating_type(str): the rating type on how feedback should be interpreted. 217 218 Returns: 219 the LensKitRecommender wrapper of ItemItem. 220 """ 221 algo = TopN( 222 lenskit_algorithms.create_item_item(params, kwargs['rating_type']), 223 create_candidate_selector(kwargs['rated_items_filter']) 224 ) 225 226 return LensKitRecommender(algo, name, params, **kwargs) 227 228 229def create_pop_score(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 230 """Create the PopScore recommender. 231 232 Args: 233 name: the name of the algorithm. 234 params: containing the following name-value pairs: 235 score_method(str): for computing popularity scores ('quantile', 'rank' or 'count'). 236 237 Keyword Args: 238 num_threads(int): the max number of threads the algorithm can use. 239 rated_items_filter(bool): whether to filter already rated items when 240 producing item recommendations. 241 242 Returns: 243 the LensKitRecommender wrapper of PopScore. 244 """ 245 algo = TopN( 246 lenskit_algorithms.create_pop_score(params), 247 create_candidate_selector(kwargs['rated_items_filter']) 248 ) 249 250 return LensKitRecommender(algo, name, params, **kwargs) 251 252 253def create_random(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 254 """Create the Random recommender. 255 256 Args: 257 name: the name of the algorithm. 258 params: containing the following name-value pairs: 259 random_seed(int): the random seed or None for the current time as seed. 260 261 Keyword Args: 262 num_threads(int): the max number of threads the algorithm can use. 263 rated_items_filter(bool): whether to filter already rated items when 264 producing item recommendations. 265 266 Returns: 267 the LensKitRecommender wrapper of Random. 268 """ 269 algo = lenskit_algorithms.create_random( 270 params, 271 create_candidate_selector(kwargs['rated_items_filter']) 272 ) 273 274 return LensKitRecommender(algo, name, params, **kwargs) 275 276 277def create_user_user(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 278 """Create the UserUser recommender. 279 280 Args: 281 name: the name of the algorithm. 282 params: containing the following name-value pairs: 283 max_neighbors(int): the maximum number of neighbors for scoring each item. 284 min_neighbors(int): the minimum number of neighbors for scoring each item. 285 min_similarity(float): minimum similarity threshold for considering a neighbor. 286 287 Keyword Args: 288 num_threads(int): the max number of threads the algorithm can use. 289 rated_items_filter(bool): whether to filter already rated items when 290 producing item recommendations. 291 rating_type(str): the rating type on how feedback should be interpreted. 292 293 Returns: 294 the LensKitRecommender wrapper of UserUser. 295 """ 296 algo = TopN( 297 lenskit_algorithms.create_user_user(params, kwargs['rating_type']), 298 create_candidate_selector(kwargs['rated_items_filter']) 299 ) 300 301 return LensKitRecommender(algo, name, params, **kwargs)
37class LensKitRecommender(Recommender): 38 """Recommender implementation for the LensKit framework.""" 39 40 def __init__(self, algo: lenskit.Recommender, name: str, params: Dict[str, Any], **kwargs): 41 """Construct the lenskit recommender. 42 43 Args: 44 algo: the lenskit recommender algorithm. 45 name: the name of the recommender. 46 params: the parameters of the recommender. 47 48 Keyword Args: 49 num_threads(int): the max number of threads the recommender can use. 50 rated_items_filter(bool): whether to filter already rated items when 51 producing item recommendations. 52 """ 53 Recommender.__init__(self, name, params, 54 kwargs['num_threads'], kwargs['rated_items_filter']) 55 self.algo = algo 56 57 def on_train(self, train_set: pd.DataFrame) -> None: 58 """Fit the lenskit algorithm on the train set. 59 60 The recommender should be trained with a dataframe matrix. 61 62 Args: 63 train_set: the set to train the recommender with. 64 65 Raises: 66 ArithmeticError: possibly raised by an algorithm on training. 67 MemoryError: possibly raised by an algorithm on training. 68 RuntimeError: possibly raised by an algorithm on training. 69 TypeError: when the train set is not a pandas dataframe. 70 """ 71 if not isinstance(train_set, pd.DataFrame): 72 raise TypeError('Expected recommender to be trained with a dataframe matrix') 73 74 self.algo.fit(train_set) 75 76 def on_recommend(self, user: int, num_items: int) -> pd.DataFrame: 77 """Compute item recommendations for the specified user. 78 79 Lenskit recommenders have an implementation that is exactly the 80 same as the required interface. 81 82 Args: 83 user: the user ID to compute recommendations for. 84 num_items: the number of item recommendations to produce. 85 86 Raises: 87 ArithmeticError: possibly raised by a recommender on testing. 88 MemoryError: possibly raised by a recommender on testing. 89 RuntimeError: when the recommender is not trained yet. 90 91 Returns: 92 dataframe with the columns: 'item' and 'score'. 93 """ 94 recs = self.algo.recommend(user, n=num_items) 95 # random algo does not produce a score 96 if self.get_name() == lenskit_algorithms.RANDOM: 97 recs['score'] = np.full(num_items, 1) 98 99 return recs 100 101 def on_recommend_batch(self, users: List[int], num_items: int) -> pd.DataFrame: 102 """Compute the items recommendations for each of the specified users. 103 104 Lenskit recommenders have a batch implementation available that allows for 105 recommending items using multiple 'jobs'. 106 107 Args: 108 users: the user ID's to compute recommendations for. 109 num_items: the number of item recommendations to produce. 110 111 Raises: 112 ArithmeticError: possibly raised by a recommender on testing. 113 MemoryError: possibly raised by a recommender on testing. 114 RuntimeError: when the recommender is not trained yet. 115 116 Returns: 117 dataframe with the columns: 'rank', 'user', 'item', 'score'. 118 """ 119 n_jobs = self.num_threads if self.num_threads > 0 else None 120 recs = batch.recommend(self.algo, users, num_items, n_jobs=n_jobs) 121 122 # random algo does not produce a score 123 if self.get_name() == lenskit_algorithms.RANDOM: 124 recs['score'] = np.full(len(users) * num_items, 1) 125 126 return recs[['rank', 'user', 'item', 'score']]
Recommender implementation for the LensKit framework.
40 def __init__(self, algo: lenskit.Recommender, name: str, params: Dict[str, Any], **kwargs): 41 """Construct the lenskit recommender. 42 43 Args: 44 algo: the lenskit recommender algorithm. 45 name: the name of the recommender. 46 params: the parameters of the recommender. 47 48 Keyword Args: 49 num_threads(int): the max number of threads the recommender can use. 50 rated_items_filter(bool): whether to filter already rated items when 51 producing item recommendations. 52 """ 53 Recommender.__init__(self, name, params, 54 kwargs['num_threads'], kwargs['rated_items_filter']) 55 self.algo = algo
Construct the lenskit recommender.
Args: algo: the lenskit recommender algorithm. 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.
57 def on_train(self, train_set: pd.DataFrame) -> None: 58 """Fit the lenskit algorithm on the train set. 59 60 The recommender should be trained with a dataframe matrix. 61 62 Args: 63 train_set: the set to train the recommender with. 64 65 Raises: 66 ArithmeticError: possibly raised by an algorithm on training. 67 MemoryError: possibly raised by an algorithm on training. 68 RuntimeError: possibly raised by an algorithm on training. 69 TypeError: when the train set is not a pandas dataframe. 70 """ 71 if not isinstance(train_set, pd.DataFrame): 72 raise TypeError('Expected recommender to be trained with a dataframe matrix') 73 74 self.algo.fit(train_set)
Fit the lenskit algorithm on the train set.
The recommender should be trained with a dataframe matrix.
Args: train_set: the set to train the recommender with.
Raises: ArithmeticError: possibly raised by an algorithm on training. MemoryError: possibly raised by an algorithm on training. RuntimeError: possibly raised by an algorithm on training. TypeError: when the train set is not a pandas dataframe.
76 def on_recommend(self, user: int, num_items: int) -> pd.DataFrame: 77 """Compute item recommendations for the specified user. 78 79 Lenskit recommenders have an implementation that is exactly the 80 same as the required interface. 81 82 Args: 83 user: the user ID to compute recommendations for. 84 num_items: the number of item recommendations to produce. 85 86 Raises: 87 ArithmeticError: possibly raised by a recommender on testing. 88 MemoryError: possibly raised by a recommender on testing. 89 RuntimeError: when the recommender is not trained yet. 90 91 Returns: 92 dataframe with the columns: 'item' and 'score'. 93 """ 94 recs = self.algo.recommend(user, n=num_items) 95 # random algo does not produce a score 96 if self.get_name() == lenskit_algorithms.RANDOM: 97 recs['score'] = np.full(num_items, 1) 98 99 return recs
Compute item recommendations for the specified user.
Lenskit recommenders have an implementation that is exactly the same as the required interface.
Args: user: the user ID to compute recommendations for. num_items: the number of item recommendations to produce.
Raises: ArithmeticError: possibly raised by a recommender on testing. MemoryError: possibly raised by a recommender on testing. RuntimeError: when the recommender is not trained yet.
Returns: dataframe with the columns: 'item' and 'score'.
101 def on_recommend_batch(self, users: List[int], num_items: int) -> pd.DataFrame: 102 """Compute the items recommendations for each of the specified users. 103 104 Lenskit recommenders have a batch implementation available that allows for 105 recommending items using multiple 'jobs'. 106 107 Args: 108 users: the user ID's to compute recommendations for. 109 num_items: the number of item recommendations to produce. 110 111 Raises: 112 ArithmeticError: possibly raised by a recommender on testing. 113 MemoryError: possibly raised by a recommender on testing. 114 RuntimeError: when the recommender is not trained yet. 115 116 Returns: 117 dataframe with the columns: 'rank', 'user', 'item', 'score'. 118 """ 119 n_jobs = self.num_threads if self.num_threads > 0 else None 120 recs = batch.recommend(self.algo, users, num_items, n_jobs=n_jobs) 121 122 # random algo does not produce a score 123 if self.get_name() == lenskit_algorithms.RANDOM: 124 recs['score'] = np.full(len(users) * num_items, 1) 125 126 return recs[['rank', 'user', 'item', 'score']]
Compute the items recommendations for each of the specified users.
Lenskit recommenders have a batch implementation available that allows for recommending items using multiple 'jobs'.
Args: users: the user ID's to compute recommendations for. num_items: the number of item recommendations to produce.
Raises: ArithmeticError: possibly raised by a recommender on testing. MemoryError: possibly raised by a recommender on testing. RuntimeError: when the recommender is not trained yet.
Returns: dataframe with the columns: 'rank', 'user', 'item', 'score'.
Inherited Members
129def create_candidate_selector(rated_items_filter: bool) -> CandidateSelector: 130 """Create a candidate selector for the specified filter. 131 132 Args: 133 rated_items_filter(bool): whether to filter already rated items when 134 producing item recommendations. 135 136 Returns: 137 the corresponding lenskit candidate selector. 138 """ 139 return UnratedItemCandidateSelector() if rated_items_filter else AllItemsCandidateSelector()
Create a candidate selector for the specified filter.
Args: rated_items_filter(bool): whether to filter already rated items when producing item recommendations.
Returns: the corresponding lenskit candidate selector.
142def create_biased_mf(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 143 """Create the BiasedMF recommender. 144 145 Args: 146 name: the name of the algorithm. 147 params: containing the following name-value pairs: 148 features(int): the number of features to train. 149 iterations(int): the number of iterations to train. 150 user_reg(float): the regularization factor for users. 151 item_reg(float): the regularization factor for items. 152 damping(float): damping factor for the underlying bias. 153 method(str): the solver to use ('cd' or 'lu'). 154 random_seed(int): the random seed or None for the current time as seed. 155 156 Keyword Args: 157 num_threads(int): the max number of threads the algorithm can use. 158 rated_items_filter(bool): whether to filter already rated items when 159 producing item recommendations. 160 161 Returns: 162 the LensKitRecommender wrapper of BiasedMF. 163 """ 164 algo = TopN( 165 lenskit_algorithms.create_biased_mf(params), 166 create_candidate_selector(kwargs['rated_items_filter']) 167 ) 168 169 return LensKitRecommender(algo, name, params, **kwargs)
Create the BiasedMF recommender.
Args: name: the name of the algorithm. 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.
Keyword Args: num_threads(int): the max number of threads the algorithm can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations.
Returns: the LensKitRecommender wrapper of BiasedMF.
172def create_implicit_mf(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 173 """Create the ImplicitMF recommender. 174 175 Args: 176 name: the name of the algorithm. 177 params: containing the following name-value pairs: 178 features(int): the number of features to train. 179 iterations(int): the number of iterations to train. 180 reg(float): the regularization factor. 181 weight(flot): the scaling weight for positive samples. 182 use_ratings(bool): whether to use the rating column or treat 183 every rated user-item pair as having a rating of 1. 184 method(str): the training method ('cg' or 'lu'). 185 random_seed(int): the random seed or None for the current time as seed. 186 187 Keyword Args: 188 num_threads(int): the max number of threads the algorithm can use. 189 rated_items_filter(bool): whether to filter already rated items when 190 producing item recommendations. 191 192 Returns: 193 the LensKitRecommender wrapper of ImplicitMF. 194 """ 195 algo = TopN( 196 lenskit_algorithms.create_implicit_mf(params), 197 create_candidate_selector(kwargs['rated_items_filter']) 198 ) 199 200 return LensKitRecommender(algo, name, params, **kwargs)
Create the ImplicitMF recommender.
Args: name: the name of the algorithm. 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.
Keyword Args: num_threads(int): the max number of threads the algorithm can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations.
Returns: the LensKitRecommender wrapper of ImplicitMF.
203def create_item_item(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 204 """Create the ItemItem recommender. 205 206 Args: 207 name: the name of the algorithm. 208 params: containing the following name-value pairs: 209 max_neighbors(int): the maximum number of neighbors for scoring each item. 210 min_neighbors(int): the minimum number of neighbors for scoring each item. 211 min_similarity(float): minimum similarity threshold for considering a neighbor. 212 213 Keyword Args: 214 num_threads(int): the max number of threads the algorithm can use. 215 rated_items_filter(bool): whether to filter already rated items when 216 producing item recommendations. 217 rating_type(str): the rating type on how feedback should be interpreted. 218 219 Returns: 220 the LensKitRecommender wrapper of ItemItem. 221 """ 222 algo = TopN( 223 lenskit_algorithms.create_item_item(params, kwargs['rating_type']), 224 create_candidate_selector(kwargs['rated_items_filter']) 225 ) 226 227 return LensKitRecommender(algo, name, params, **kwargs)
Create the ItemItem recommender.
Args: name: the name of the algorithm. 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.
Keyword Args: num_threads(int): the max number of threads the algorithm can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations. rating_type(str): the rating type on how feedback should be interpreted.
Returns: the LensKitRecommender wrapper of ItemItem.
230def create_pop_score(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 231 """Create the PopScore recommender. 232 233 Args: 234 name: the name of the algorithm. 235 params: containing the following name-value pairs: 236 score_method(str): for computing popularity scores ('quantile', 'rank' or 'count'). 237 238 Keyword Args: 239 num_threads(int): the max number of threads the algorithm can use. 240 rated_items_filter(bool): whether to filter already rated items when 241 producing item recommendations. 242 243 Returns: 244 the LensKitRecommender wrapper of PopScore. 245 """ 246 algo = TopN( 247 lenskit_algorithms.create_pop_score(params), 248 create_candidate_selector(kwargs['rated_items_filter']) 249 ) 250 251 return LensKitRecommender(algo, name, params, **kwargs)
Create the PopScore recommender.
Args: name: the name of the algorithm. params: containing the following name-value pairs: score_method(str): for computing popularity scores ('quantile', 'rank' or 'count').
Keyword Args: num_threads(int): the max number of threads the algorithm can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations.
Returns: the LensKitRecommender wrapper of PopScore.
254def create_random(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 255 """Create the Random recommender. 256 257 Args: 258 name: the name of the algorithm. 259 params: containing the following name-value pairs: 260 random_seed(int): the random seed or None for the current time as seed. 261 262 Keyword Args: 263 num_threads(int): the max number of threads the algorithm can use. 264 rated_items_filter(bool): whether to filter already rated items when 265 producing item recommendations. 266 267 Returns: 268 the LensKitRecommender wrapper of Random. 269 """ 270 algo = lenskit_algorithms.create_random( 271 params, 272 create_candidate_selector(kwargs['rated_items_filter']) 273 ) 274 275 return LensKitRecommender(algo, name, params, **kwargs)
Create the Random recommender.
Args: name: the name of the algorithm. params: containing the following name-value pairs: random_seed(int): the random seed or None for the current time as seed.
Keyword Args: num_threads(int): the max number of threads the algorithm can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations.
Returns: the LensKitRecommender wrapper of Random.
278def create_user_user(name: str, params: Dict[str, Any], **kwargs) -> LensKitRecommender: 279 """Create the UserUser recommender. 280 281 Args: 282 name: the name of the algorithm. 283 params: containing the following name-value pairs: 284 max_neighbors(int): the maximum number of neighbors for scoring each item. 285 min_neighbors(int): the minimum number of neighbors for scoring each item. 286 min_similarity(float): minimum similarity threshold for considering a neighbor. 287 288 Keyword Args: 289 num_threads(int): the max number of threads the algorithm can use. 290 rated_items_filter(bool): whether to filter already rated items when 291 producing item recommendations. 292 rating_type(str): the rating type on how feedback should be interpreted. 293 294 Returns: 295 the LensKitRecommender wrapper of UserUser. 296 """ 297 algo = TopN( 298 lenskit_algorithms.create_user_user(params, kwargs['rating_type']), 299 create_candidate_selector(kwargs['rated_items_filter']) 300 ) 301 302 return LensKitRecommender(algo, name, params, **kwargs)
Create the UserUser recommender.
Args: name: the name of the algorithm. 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.
Keyword Args: num_threads(int): the max number of threads the algorithm can use. rated_items_filter(bool): whether to filter already rated items when producing item recommendations. rating_type(str): the rating type on how feedback should be interpreted.
Returns: the LensKitRecommender wrapper of UserUser.