src.fairreckitlib.model.pipeline.recommendation_pipeline
This module contains a model pipeline that recommends items based on rating predictions.
Classes:
RecommendationPipeline: can batch recommendations from multiple models for a specific API.
RecommendationPipelineCSR: recommendation pipeline with a csr matrix instead of a dataframe.
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 a model pipeline that recommends items based on rating predictions. 2 3Classes: 4 5 RecommendationPipeline: can batch recommendations from multiple models for a specific API. 6 RecommendationPipelineCSR: recommendation pipeline with a csr matrix instead of a dataframe. 7 8This program has been developed by students from the bachelor Computer Science at 9Utrecht University within the Software Project course. 10© Copyright Utrecht University (Department of Information and Computing Sciences) 11""" 12 13from typing import List 14 15import pandas as pd 16 17from ..algorithms.base_recommender import BaseRecommender 18from ..algorithms.matrix import Matrix, MatrixCSR 19from .model_pipeline import ModelPipeline 20 21 22class RecommendationPipeline(ModelPipeline): 23 """Recommendation Pipeline that computes item recommendations. 24 25 The topK item recommendations will be computed for each user that is present in the test set. 26 """ 27 28 def load_test_set_users(self) -> None: 29 """Load the test set users that all models can use for testing. 30 31 Recommendations are made for every user in the test set. 32 33 Raises: 34 FileNotFoundError: when the test set file is not found. 35 """ 36 test_set = self.load_test_set_dataframe('recommendation test set') 37 self.test_set_users = test_set['user'].unique() 38 39 def test_model_ratings( 40 self, 41 model: BaseRecommender, 42 user_batch: List[int], 43 **kwargs) -> pd.DataFrame: 44 """Test the specified model for rating recommendations. 45 46 Produce a top K number of item scores for each user that is present in the test set. 47 48 Args: 49 model: the model that needs to be tested. 50 user_batch: the user batch to compute model ratings for. 51 52 Keyword Args: 53 num_items(int): the number of item recommendations to produce. 54 55 Raises: 56 ArithmeticError: possibly raised by a recommender model on testing. 57 MemoryError: possibly raised by a recommender model on testing. 58 RuntimeError: possibly raised by a recommender model on testing. 59 60 Returns: 61 a dataframe containing the computed item recommendations. 62 """ 63 return model.recommend_batch(user_batch, num_items=kwargs['num_items']) 64 65 66class RecommendationPipelineCSR(RecommendationPipeline): 67 """Recommendation Pipeline implementation for a CSR matrix train set.""" 68 69 def on_load_train_set_matrix(self) -> Matrix: 70 """Load the train set matrix that all models can use for training. 71 72 Raises: 73 FileNotFoundError: when the train set file is not found. 74 75 Returns: 76 the loaded train set csr matrix. 77 """ 78 return MatrixCSR(self.data_transition.train_set_path)
23class RecommendationPipeline(ModelPipeline): 24 """Recommendation Pipeline that computes item recommendations. 25 26 The topK item recommendations will be computed for each user that is present in the test set. 27 """ 28 29 def load_test_set_users(self) -> None: 30 """Load the test set users that all models can use for testing. 31 32 Recommendations are made for every user in the test set. 33 34 Raises: 35 FileNotFoundError: when the test set file is not found. 36 """ 37 test_set = self.load_test_set_dataframe('recommendation test set') 38 self.test_set_users = test_set['user'].unique() 39 40 def test_model_ratings( 41 self, 42 model: BaseRecommender, 43 user_batch: List[int], 44 **kwargs) -> pd.DataFrame: 45 """Test the specified model for rating recommendations. 46 47 Produce a top K number of item scores for each user that is present in the test set. 48 49 Args: 50 model: the model that needs to be tested. 51 user_batch: the user batch to compute model ratings for. 52 53 Keyword Args: 54 num_items(int): the number of item recommendations to produce. 55 56 Raises: 57 ArithmeticError: possibly raised by a recommender model on testing. 58 MemoryError: possibly raised by a recommender model on testing. 59 RuntimeError: possibly raised by a recommender model on testing. 60 61 Returns: 62 a dataframe containing the computed item recommendations. 63 """ 64 return model.recommend_batch(user_batch, num_items=kwargs['num_items'])
Recommendation Pipeline that computes item recommendations.
The topK item recommendations will be computed for each user that is present in the test set.
29 def load_test_set_users(self) -> None: 30 """Load the test set users that all models can use for testing. 31 32 Recommendations are made for every user in the test set. 33 34 Raises: 35 FileNotFoundError: when the test set file is not found. 36 """ 37 test_set = self.load_test_set_dataframe('recommendation test set') 38 self.test_set_users = test_set['user'].unique()
Load the test set users that all models can use for testing.
Recommendations are made for every user in the test set.
Raises: FileNotFoundError: when the test set file is not found.
40 def test_model_ratings( 41 self, 42 model: BaseRecommender, 43 user_batch: List[int], 44 **kwargs) -> pd.DataFrame: 45 """Test the specified model for rating recommendations. 46 47 Produce a top K number of item scores for each user that is present in the test set. 48 49 Args: 50 model: the model that needs to be tested. 51 user_batch: the user batch to compute model ratings for. 52 53 Keyword Args: 54 num_items(int): the number of item recommendations to produce. 55 56 Raises: 57 ArithmeticError: possibly raised by a recommender model on testing. 58 MemoryError: possibly raised by a recommender model on testing. 59 RuntimeError: possibly raised by a recommender model on testing. 60 61 Returns: 62 a dataframe containing the computed item recommendations. 63 """ 64 return model.recommend_batch(user_batch, num_items=kwargs['num_items'])
Test the specified model for rating recommendations.
Produce a top K number of item scores for each user that is present in the test set.
Args: model: the model that needs to be tested. user_batch: the user batch to compute model ratings for.
Keyword Args: num_items(int): the number of item recommendations to produce.
Raises: ArithmeticError: possibly raised by a recommender model on testing. MemoryError: possibly raised by a recommender model on testing. RuntimeError: possibly raised by a recommender model on testing.
Returns: a dataframe containing the computed item recommendations.
Inherited Members
- src.fairreckitlib.model.pipeline.model_pipeline.ModelPipeline
- ModelPipeline
- run
- run_model
- begin_model
- create_model_output_dir
- get_model_output_dir
- end_model
- on_load_train_set_matrix
- load_train_set_matrix
- load_train_set_dataframe
- load_test_set_dataframe
- reconstruct_ratings
- test_model
- train_model
- train_and_test_model
67class RecommendationPipelineCSR(RecommendationPipeline): 68 """Recommendation Pipeline implementation for a CSR matrix train set.""" 69 70 def on_load_train_set_matrix(self) -> Matrix: 71 """Load the train set matrix that all models can use for training. 72 73 Raises: 74 FileNotFoundError: when the train set file is not found. 75 76 Returns: 77 the loaded train set csr matrix. 78 """ 79 return MatrixCSR(self.data_transition.train_set_path)
Recommendation Pipeline implementation for a CSR matrix train set.
70 def on_load_train_set_matrix(self) -> Matrix: 71 """Load the train set matrix that all models can use for training. 72 73 Raises: 74 FileNotFoundError: when the train set file is not found. 75 76 Returns: 77 the loaded train set csr matrix. 78 """ 79 return MatrixCSR(self.data_transition.train_set_path)
Load the train set matrix that all models can use for training.
Raises: FileNotFoundError: when the train set file is not found.
Returns: the loaded train set csr matrix.