baserec.base package

Submodules

baserec.base.base_matrix_factorization_recommender module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.base_matrix_factorization_recommender.BaseMatrixFactorizationRecommender(URM_train, verbose=True)

Bases: baserec.base.base_recommender.BaseRecommender

This class refers to a BaseRecommender KNN which uses matrix factorization, it provides functions to compute item’s score as well as a function to save the W_matrix

The prediction for cold users will always be -inf for ALL items

save_model(folder_path, file_name=None)

baserec.base.base_recommender module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.base_recommender.BaseRecommender(URM_train, verbose=True)

Bases: object

Abstract BaseRecommender

RECOMMENDER_NAME = 'Recommender_Base_Class'
fit()
get_URM_train()
load_model(folder_path, file_name=None)
recommend(user_id_array: numpy.array, cutoff: Optional[int] = None, remove_seen_flag: bool = True, items_to_compute: Optional[numpy.array] = None, remove_top_pop_flag: bool = False, remove_custom_items_flag: bool = False, return_scores: bool = False) → Union[List[List[int]], List[int], numpy.array]

Make recommendations for users in user_id_array

Parameters
  • user_id_array (np.array) – List of user_ids to make recommendations for

  • cutoff (Optional[int], optional) – [description], by default None

  • remove_seen_flag (bool, optional) – Don’t recommend seen items, by default True

  • items_to_compute (np.array, optional) – List of item_ids to include in computation, by default None

  • remove_top_pop_flag (bool, optional) – Don’t recommend top popular items, by default False

  • remove_custom_items_flag (bool, optional) – [description], by default False

  • return_scores (bool, optional) – Also return the score matrix, by default False

Returns

ranking_list or (ranking_list, scores_batch) accorgin to return_scores

Return type

Union[List, Tuple[List, np.array]]

reset_items_to_ignore()
save_model(folder_path, file_name=None)
set_URM_train(URM_train_new, **kwargs)
set_items_to_ignore(items_to_ignore)

baserec.base.base_similarity_matrix_recommender module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.base_similarity_matrix_recommender.BaseItemSimilarityMatrixRecommender(URM_train, verbose=True)

Bases: baserec.base.base_similarity_matrix_recommender.BaseSimilarityMatrixRecommender

class baserec.base.base_similarity_matrix_recommender.BaseSimilarityMatrixRecommender(URM_train, verbose=True)

Bases: baserec.base.base_recommender.BaseRecommender

This class refers to a BaseRecommender KNN which uses a similarity matrix, it provides two function to compute item’s score bot for user-based and Item-based models as well as a function to save the W_matrix

save_model(folder_path, file_name=None)

Only one parameter — W_sparse — to save.

class baserec.base.base_similarity_matrix_recommender.BaseUserSimilarityMatrixRecommender(URM_train, verbose=True)

Bases: baserec.base.base_similarity_matrix_recommender.BaseSimilarityMatrixRecommender

baserec.base.base_temp_folder module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.base_temp_folder.BaseTempFolder

Bases: object

baserec.base.data_io module

Created on 23/10/17

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.data_io.DataIO(folder_path)

Bases: object

load_data(file_name)
save_data(file_name, data_dict_to_save)
baserec.base.data_io.json_not_serializable_handler(o)

Json cannot serialize automatically some data types, for example numpy integers (int32). This may be a limitation of numpy-json interfaces for Python 3.6 and may not occur in Python 3.7

baserec.base.incremental_training_early_stopping module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.incremental_training_early_stopping.IncrementalTrainingEarlyStopping

Bases: object

This class provides a function which trains a model applying early stopping

The term “incremental” refers to the model that is updated at every epoch The term “best” refers to the incremental model which corresponded to the best validation score

The object must implement the following methods:

_run_epoch(self, num_epoch) : trains the model for one epoch (e.g. calling another object implementing the training cython, pyTorch…)

_prepare_model_for_validation(self) : ensures the recommender being trained can compute the predictions needed for the validation step

_update_best_model(self) : updates the best model with the current incremental one

_train_with_early_stopping(.) : Function that executes the training, validation and early stopping by using the previously implemented functions

get_early_stopping_final_epochs_dict()

This function returns a dictionary to be used as optimal parameters in the .fit() function It provides the flexibility to deal with multiple early-stopping in a single algorithm e.g. in NeuMF there are three model components each with its own optimal number of epochs the return dict would be {“epochs”: epochs_best_neumf, “epochs_gmf”: epochs_best_gmf, “epochs_mlp”: epochs_best_mlp} :return:

baserec.base.ir_feature_weighting module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

baserec.base.ir_feature_weighting.TF_IDF(dataMatrix)

Items are assumed to be on rows :param dataMatrix: :return:

baserec.base.ir_feature_weighting.okapi_BM_25(dataMatrix, K1=1.2, B=0.75)

Items are assumed to be on rows :param dataMatrix: :param K1: :param B: :return:

baserec.base.non_personalized_recommenders module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.non_personalized_recommenders.GlobalEffects(URM_train)

Bases: baserec.base.base_recommender.BaseRecommender

docstring for GlobalEffects

RECOMMENDER_NAME = 'GlobalEffectsRecommender'
fit(lambda_user=10, lambda_item=25)
save_model(folder_path, file_name=None)
class baserec.base.non_personalized_recommenders.Random(URM_train)

Bases: baserec.base.base_recommender.BaseRecommender

Random recommender

RECOMMENDER_NAME = 'RandomRecommender'
fit(random_seed=42)
save_model(folder_path, file_name=None)
class baserec.base.non_personalized_recommenders.TopPop(URM_train)

Bases: baserec.base.base_recommender.BaseRecommender

Top Popular recommender

RECOMMENDER_NAME = 'TopPopRecommender'
fit()
save_model(folder_path, file_name=None)

baserec.base.recommender_utils module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

baserec.base.recommender_utils.addZeroSamples(S_matrix, numSamplesToAdd)
baserec.base.recommender_utils.areURMequals(URM1, URM2)
baserec.base.recommender_utils.check_matrix(X, format='csc', dtype=<class 'numpy.float32'>)

This function takes a matrix as input and transforms it into the specified format. The matrix in input can be either sparse or ndarray. If the matrix in input has already the desired format, it is returned as-is the dtype parameter is always applied and the default is np.float32 :param X: :param format: :param dtype: :return:

baserec.base.recommender_utils.get_unique_temp_folder(input_temp_folder_path)

The function returns the path of a folder in result_experiments The function guarantees that the folder is not already existent and it creates it :return:

baserec.base.recommender_utils.removeTopPop(URM_1, URM_2=None, percentageToRemove=0.2)

Remove the top popular items from the matrix :param URM_1: user X items :param URM_2: user X items :param percentageToRemove: value 1 corresponds to 100% :return: URM: user X selectedItems, obtained from URM_1

Array: itemMappings[selectedItemIndex] = originalItemIndex Array: removedItems

baserec.base.recommender_utils.reshapeSparse(sparseMatrix, newShape)
baserec.base.recommender_utils.similarityMatrixTopK(item_weights, k=100, verbose=False)

The function selects the TopK most similar elements, column-wise

Parameters
  • item_weights

  • forceSparseOutput

  • k

  • verbose

  • inplace – Default True, WARNING matrix will be modified

Returns

baserec.base.recommender_utils_test module

@author: Maurizio Ferrari Dacrema & Ceshine Lee

class baserec.base.recommender_utils_test.MyTestCase(methodName='runTest')

Bases: unittest.case.TestCase

test_similarityMatrixTopK_denseToDense()
test_similarityMatrixTopK_sparseToSparse()

Module contents