src.recommender package

Submodules

src.recommender.metrics module

The metrics functions are copied from this repository: https://gist.github.com/bwhite/3726239

src.recommender.metrics.dcg_at_k(r, k, method=0)[source]

Score is discounted cumulative gain (dcg) Relevance is positive real values. Can use binary as the previous methods. Example from http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf >>> r = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0] >>> dcg_at_k(r, 1) 3.0 >>> dcg_at_k(r, 1, method=1) 3.0 >>> dcg_at_k(r, 2) 5.0 >>> dcg_at_k(r, 2, method=1) 4.2618595071429155 >>> dcg_at_k(r, 10) 9.6051177391888114 >>> dcg_at_k(r, 11) 9.6051177391888114 :param r: Relevance scores (list or numpy) in rank order

(first element is the first item)
Parameters:
  • k – Number of results to consider
  • method – If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, …] If 1 then weights are [1.0, 0.6309, 0.5, 0.4307, …]
Returns:

Discounted cumulative gain

src.recommender.metrics.evaluate_recommendations(predictions, target, k)[source]

Evaluate the quality of recommendations with NDCG. We compare the predictions set with the target set that should reflect what items are relevant.

Parameters:
  • predictions (list) – List of recommended items. Ordered by descending score.
  • target (list) – List of relevant items.
  • k (int) – Only consider the k first items in the set
Returns:

NDCG at k score

Return type:

float

src.recommender.metrics.ndcg_at_k(r, k, method=0)[source]

Score is normalized discounted cumulative gain (ndcg) Relevance is positive real values. Can use binary as the previous methods. Example from http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf >>> r = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0] >>> ndcg_at_k(r, 1) 1.0 >>> r = [2, 1, 2, 0] >>> ndcg_at_k(r, 4) 0.9203032077642922 >>> ndcg_at_k(r, 4, method=1) 0.96519546960144276 >>> ndcg_at_k([0], 1) 0.0 >>> ndcg_at_k([1], 2) 1.0 :param r: Relevance scores (list or numpy) in rank order

(first element is the first item)
Parameters:
  • k – Number of results to consider
  • method – If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, …] If 1 then weights are [1.0, 0.6309, 0.5, 0.4307, …]
Returns:

Normalized discounted cumulative gain

src.recommender.recommender module

class src.recommender.recommender.Recommender[source]

Bases: object

Recommender System base class.

recommend(context, restrict_to_engines=[])[source]

Call all the active engines based on a context and return their recommendations.

It is possible to restrict to a list of engines by using the restrict_to_engines parameter.

Parameters:context (recommender.wrappers.Context) – Context wrapper, providing informations about the current item or user or session.
Returns:List of recommendations as dictionaries
Return type:list(dict)

src.recommender.wrappers module

class src.recommender.wrappers.Context(**kwargs)[source]

Bases: object

A wrapper for context that will help engines make recommendations.

class src.recommender.wrappers.Recommendations[source]

Bases: object

A recommendation object that is returned by the engines

to_dict()[source]

Convert recommendation object to dict

Returns:the recommendations as a dictionary
Return type:dict
to_string()[source]

Convert recommendation object to string for debug purpose

Returns:the recommendations as string, stating the type and number of items recommended.
Return type:str