Evaluation

implicit.evaluation.AUC_at_k(model, train_user_items, test_user_items, int K=10, show_progress=True, int num_threads=1)

Calculate limited AUC for a given trained model

Parameters
  • model (RecommenderBase) – The fitted recommendation model to test

  • train_user_items (csr_matrix) – Sparse matrix of user by item that contains elements that were used in training the model

  • test_user_items (csr_matrix) – Sparse matrix of user by item that contains withheld elements to test on

  • K (int) – Number of items to test on

  • show_progress (bool, optional) – Whether to show a progress bar

  • num_threads (int, optional) – The number of threads to use for testing. Specifying 0 means to default to the number of cores on the machine. Note: aside from the ALS and BPR models, setting this to more than 1 will likely hurt performance rather than help.

Returns

the calculated ndcg@k

Return type

float

implicit.evaluation.leave_k_out_split(ratings, int K=1, float train_only_size=0.0, random_state=None)

Implements the ‘leave-k-out’ split protocol for a ratings matrix. Default parameters will produce a ‘leave-one-out’ split.

This will create two matrices, one where each eligible user (i.e. user with > K + 1 ratings) will have a single rating held in the test set, and all other ratings held in the train set. Optionally, a percentage of users can be reserved to appear _only_ in the train set. By default, all eligible users may appear in the test set.

Parameters
  • ratings (csr_matrix) – The input ratings CSR matrix to be split.

  • K (int) – The total number of samples to be ‘left out’ in the test set.

  • train_only_size (float) – The size (as a fraction) of the users set that should appear only in the training matrix.

  • random_state (int, None or RandomState) – The existing RandomState. If None, or an int, will be used to seed a new numpy RandomState.

Returns

(train, test) – A tuple of CSR matrix corresponding to training/testing matrices.

Return type

csr_matrix, csr_matrix

implicit.evaluation.mean_average_precision_at_k(model, train_user_items, test_user_items, int K=10, show_progress=True, int num_threads=1)

Calculates MAP@K for a given trained model

Parameters
  • model (RecommenderBase) – The fitted recommendation model to test

  • train_user_items (csr_matrix) – Sparse matrix of user by item that contains elements that were used in training the model

  • test_user_items (csr_matrix) – Sparse matrix of user by item that contains withheld elements to test on

  • K (int) – Number of items to test on

  • show_progress (bool, optional) – Whether to show a progress bar

  • num_threads (int, optional) – The number of threads to use for testing. Specifying 0 means to default to the number of cores on the machine. Note: aside from the ALS and BPR models, setting this to more than 1 will likely hurt performance rather than help.

Returns

the calculated MAP@k

Return type

float

implicit.evaluation.ndcg_at_k(model, train_user_items, test_user_items, int K=10, show_progress=True, int num_threads=1)

Calculates ndcg@K for a given trained model

Parameters
  • model (RecommenderBase) – The fitted recommendation model to test

  • train_user_items (csr_matrix) – Sparse matrix of user by item that contains elements that were used in training the model

  • test_user_items (csr_matrix) – Sparse matrix of user by item that contains withheld elements to test on

  • K (int) – Number of items to test on

  • show_progress (bool, optional) – Whether to show a progress bar

  • num_threads (int, optional) – The number of threads to use for testing. Specifying 0 means to default to the number of cores on the machine. Note: aside from the ALS and BPR models, setting this to more than 1 will likely hurt performance rather than help.

Returns

the calculated ndcg@k

Return type

float

implicit.evaluation.precision_at_k(model, train_user_items, test_user_items, int K=10, show_progress=True, int num_threads=1)

Calculates P@K for a given trained model

Parameters
  • model (RecommenderBase) – The fitted recommendation model to test

  • train_user_items (csr_matrix) –

    Sparse matrix of user by item that contains elements that were used

    in training the model

  • test_user_items (csr_matrix) – Sparse matrix of user by item that contains withheld elements to test on

  • K (int) – Number of items to test on

  • show_progress (bool, optional) – Whether to show a progress bar

  • num_threads (int, optional) – The number of threads to use for testing. Specifying 0 means to default to the number of cores on the machine. Note: aside from the ALS and BPR models, setting this to more than 1 will likely hurt performance rather than help.

Returns

the calculated p@k

Return type

float

implicit.evaluation.ranking_metrics_at_k(model, train_user_items, test_user_items, int K=10, show_progress=True, int num_threads=1)

Calculates ranking metrics for a given trained model

Parameters
  • model (RecommenderBase) – The fitted recommendation model to test

  • train_user_items (csr_matrix) –

    Sparse matrix of user by item that contains elements that were used

    in training the model

  • test_user_items (csr_matrix) – Sparse matrix of user by item that contains withheld elements to test on

  • K (int) – Number of items to test on

  • show_progress (bool, optional) – Whether to show a progress bar

  • num_threads (int, optional) – The number of threads to use for testing. Specifying 0 means to default to the number of cores on the machine. Note: aside from the ALS and BPR models, setting this to more than 1 will likely hurt performance rather than help.

Returns

the calculated p@k

Return type

float

implicit.evaluation.train_test_split(ratings, train_percentage=0.8, random_state=None)

Randomly splits the ratings matrix into two matrices for training/testing.

Parameters
  • ratings (coo_matrix) – A sparse matrix to split

  • train_percentage (float) – What percentage of ratings should be used for training

  • random_state (int, None or RandomState) – The existing RandomState. If None, or an int, will be used to seed a new numpy RandomState.

Returns

(train, test) – A tuple of csr_matrices for training/testing

Return type

csr_matrix, csr_matrix