src.recommender.engines package

This is the engines package, where we define the recommendation engines.

This package contains the following modules:

  • engine: Module where the base classes are defined. All the created
    engines should inherit from one of these base classes. They define the skeleton of the engine, like which methods they must overwrite.
  • collaborative_filtering: All collaborative filtering engines
  • content_based: All content based filtering engines
  • generic: All the generic engines, that are not really collaborative
    or content based (e.g display the most popular items, or the items in the user browsing history, etc …)

Submodules

src.recommender.engines.collaborative_filtering module

class src.recommender.engines.collaborative_filtering.Item2Vec[source]

Bases: src.recommender.engines.engine.OfflineEngine

train()[source]

Method for training the engine. This method should load the dataset, compute the recommendations and then persist them to disk using save_recommendations_to_csv.

class src.recommender.engines.collaborative_filtering.Item2VecOnline[source]

Bases: src.recommender.engines.engine.OnlineEngine

load_model()[source]

Load the ML model from disk and return it

Returns:The ML model to be saved as self.model
predict(context)[source]

Predict using the loaded model and the context.

Parameters:context (src.recommender.wrappers.Context) – Context wrapper
Returns:list of recommended ids sorted by descending score scores (list(float)): list of scores for each recommended item
Return type:ids (list(int))
train()[source]

Train a ML model and save it to disk

class src.recommender.engines.collaborative_filtering.ItemBasedCF[source]

Bases: src.recommender.engines.engine.OfflineEngine

train()[source]

Method for training the engine. This method should load the dataset, compute the recommendations and then persist them to disk using save_recommendations_to_csv.

src.recommender.engines.content_based module

class src.recommender.engines.content_based.OneHotMultiInput[source]

Bases: src.recommender.engines.engine.OfflineEngine

train()[source]

Method for training the engine. This method should load the dataset, compute the recommendations and then persist them to disk using save_recommendations_to_csv.

class src.recommender.engines.content_based.SameGenres[source]

Bases: src.recommender.engines.engine.QueryBasedEngine

compute_query(context)[source]

Abstract method that computes the SQL query using SQLAlchemy

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:query result
class src.recommender.engines.content_based.TfidfGenres[source]

Bases: src.recommender.engines.engine.OfflineEngine

train()[source]

Method for training the engine. This method should load the dataset, compute the recommendations and then persist them to disk using save_recommendations_to_csv.

src.recommender.engines.engine module

class src.recommender.engines.engine.Engine[source]

Bases: abc.ABC

Abstract class for all engines. You should not directly use this class, instead use the classes that inherit from this class.

init_recommendations(context)[source]

Create an empty src.recommender.wrappers.Recommendations` object and fill in the engine type, display name and priority based on the informations stored in DB.

Parameters:context (src.recommender.wrappers.Context) – Context wrapper, containing useful informations for the engine.
Returns:Recommendations object filled with engine type, display name and priority
Return type:(src.recommender.wrappers.Recommendations)
recommend(context)[source]

Abstract method for all engines for recommending items.

The context wrapper stores all the informations the engine might need to compute the recommendations, like the current item_id, the current user_id, the user browsing history, etc …

Every engine must override this method. They have to call self.init_recommendations first to create an empty src.recommender.wrappers.Recommendations object and then enrich it with the recommended items.

Parameters:context (src.recommender.wrappers.Context) – the context
Returns:the recommendation object
Return type:src.recommender.wrappers.Recommendations
class src.recommender.engines.engine.OfflineEngine[source]

Bases: src.recommender.engines.engine.QueryBasedEngine

These engines are a special kind of QueryBasedEngine because they require a training.

Most of the offline Machine Learning algorithms will inherit from this class.

The recommendations are computed offline with the train method, then saved on disk with save_recommendations_to_csv and finally uploaded to the DB using upload.

compute_query(context)[source]

Get the recommended items from the DB.

Parameters:context (src.recommender.wrappers.Context) – Context wrapper
Returns:list of Recommendation
Return type:list
save_recommendations_to_csv(recommendations)[source]

Save recommendations to a CSV file.

Parameters:recommendations (list(tuple)) – List of recommendation tuple corresponding to: (movie_id, recommended_movie_id, input_kind, score)
train()[source]

Method for training the engine. This method should load the dataset, compute the recommendations and then persist them to disk using save_recommendations_to_csv.

upload()[source]

Upload the recommendations from a CSV file to the DB.

class src.recommender.engines.engine.OnlineEngine[source]

Bases: src.recommender.engines.engine.Engine

Online Machine Learning Engines that do not get their recommendations from a SQL query but from a loaded model.

The model is trained with the train method, and loaded at runtime with the load_model method.

load_model()[source]

Load the ML model from disk and return it

Returns:The ML model to be saved as self.model
predict(context)[source]

Predict using the loaded model and the context.

Parameters:context (src.recommender.wrappers.Context) – Context wrapper
Returns:list of recommended ids sorted by descending score scores (list(float)): list of scores for each recommended item
Return type:ids (list(int))
recommend(context)[source]

Recommend movies based on context

Parameters:context (src.recommender.wrappers.Context) – Context wrapper
Returns:src.recommender.wrappers.Recommendations as dict
Return type:recommendations (dict)
train()[source]

Train a ML model and save it to disk

class src.recommender.engines.engine.QueryBasedEngine[source]

Bases: src.recommender.engines.engine.Engine

Abstract class for an engine based on a SQL query performed at every call. These are engines require no training, for instance an engine that will recommend random items for DB.

compute_query(context)[source]

Abstract method that computes the SQL query using SQLAlchemy

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:query result
recommend(context)[source]

Method for recommending items, by calling self.compute_query.

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:recommendations as list of dict
Return type:list(dict)

src.recommender.engines.generic module

class src.recommender.engines.generic.MostRecent[source]

Bases: src.recommender.engines.engine.QueryBasedEngine

compute_query(context)[source]

Abstract method that computes the SQL query using SQLAlchemy

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:query result
class src.recommender.engines.generic.Random[source]

Bases: src.recommender.engines.engine.QueryBasedEngine

compute_query(context)[source]

Abstract method that computes the SQL query using SQLAlchemy

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:query result
class src.recommender.engines.generic.TopRated[source]

Bases: src.recommender.engines.engine.QueryBasedEngine

compute_query(context)[source]

Abstract method that computes the SQL query using SQLAlchemy

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:query result
class src.recommender.engines.generic.UserHistory[source]

Bases: src.recommender.engines.engine.QueryBasedEngine

compute_query(context)[source]

Abstract method that computes the SQL query using SQLAlchemy

Parameters:context (recommender.wrappers.Context) – context wrapper
Returns:query result