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 enginescontent_based: All content based filtering enginesgeneric: 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.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))
-
src.recommender.engines.content_based module¶
-
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
-
src.recommender.engines.engine module¶
-
class
src.recommender.engines.engine.Engine[source]¶ Bases:
abc.ABCAbstract 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_recommendationsfirst to create an emptysrc.recommender.wrappers.Recommendationsobject 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.QueryBasedEngineThese 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
trainmethod, then saved on disk withsave_recommendations_to_csvand finally uploaded to the DB usingupload.-
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)
-
-
class
src.recommender.engines.engine.OnlineEngine[source]¶ Bases:
src.recommender.engines.engine.EngineOnline Machine Learning Engines that do not get their recommendations from a SQL query but from a loaded model.
The model is trained with the
trainmethod, and loaded at runtime with theload_modelmethod.-
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)
-
-
class
src.recommender.engines.engine.QueryBasedEngine[source]¶ Bases:
src.recommender.engines.engine.EngineAbstract 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
-