recpack.algorithms.Algorithm

class recpack.algorithms.Algorithm

Base class for all recpack algorithm implementations.

This class provides fit and predict methods that handle generic pre and post-conditions. Usually a new algorithm will have to implement just the _fit() and _predict() methods.

Methods

_assert_has_timestamps(*matrices)

Make sure that the matrices all have timestamp information.

_assert_is_interaction_matrix(*matrices)

Make sure that the passed matrices are all an InteractionMatrix.

_check_fit_complete()

Helper function to check if model was correctly fitted

_check_prediction(X_pred, X)

Checks that the predictions matches expectations

_fit(X)

Stub implementation for fitting an algorithm.

_predict(X)

Stub for predicting scores to users

_transform_fit_input(X)

Transform the training data to expected type

_transform_predict_input(X)

Transform the input of predict to expected type

fit(X)

Fit the model to the input interaction matrix.

predict(X)

Predicts scores, given the interactions in X

set_params(**params)

Set the parameters of the estimator.

Attributes

identifier

Name of the object.

name

Name of the object's class.

_assert_has_timestamps(*matrices: recpack.matrix.interaction_matrix.InteractionMatrix)

Make sure that the matrices all have timestamp information.

_assert_is_interaction_matrix(*matrices: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]) None

Make sure that the passed matrices are all an InteractionMatrix.

_check_fit_complete()

Helper function to check if model was correctly fitted

Uses the sklear check_is_fitted function, https://scikit-learn.org/stable/modules/generated/sklearn.utils.validation.check_is_fitted.html

_check_prediction(X_pred: scipy.sparse._csr.csr_matrix, X: scipy.sparse._csr.csr_matrix) None

Checks that the predictions matches expectations

Checks implemented:

  • Check that all users with history got at least 1 recommendation

For failing checks a warning is printed.

Parameters
  • X_pred (csr_matrix) – Predictions made for all nonzero users in X

  • X (csr_matrix) – User-item interaction matrix used as input to predict

_fit(X: scipy.sparse._csr.csr_matrix)

Stub implementation for fitting an algorithm.

Will be called by the fit() wrapper. Child classes should implement this function.

Parameters

X (csr_matrix) – User-item interaction matrix to fit the model to

Raises

NotImplementedError – Implement this method in the child class

_predict(X: scipy.sparse._csr.csr_matrix) scipy.sparse._csr.csr_matrix

Stub for predicting scores to users

Will be called by the predict() wrapper. Child classes should implement this function.

Parameters

X (csr_matrix) – User-item interaction matrix used as input to predict

Raises

NotImplementedError – Implement this method in the child class

Returns

Predictions made for all nonzero users in X

Return type

csr_matrix

_transform_fit_input(X: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]) scipy.sparse._csr.csr_matrix

Transform the training data to expected type

Data will be turned into a binary csr matrix.

Parameters

X (Matrix) – User-item interaction matrix to fit the model to

Returns

Transformed user-item interaction matrix to fit the model

Return type

csr_matrix

_transform_predict_input(X: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]) scipy.sparse._csr.csr_matrix

Transform the input of predict to expected type

Data will be turned into a binary csr matrix.

Parameters

X (Matrix) – User-item interaction matrix used as input to predict

Returns

Transformed user-item interaction matrix used as input to predict

Return type

csr_matrix

fit(X: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]) recpack.algorithms.base.Algorithm

Fit the model to the input interaction matrix.

After fitting the model will be ready to use for prediction.

This function will handle some generic bookkeeping for each of the child classes,

Parameters

X (Matrix) – The interactions to fit the model on.

Returns

self, fitted algorithm

Return type

Algorithm

property identifier

Name of the object.

Name is made by combining the class name with the parameters passed at construction time.

Constructed by recreating the initialisation call. Example: Algorithm(param_1=value)

property name

Name of the object’s class.

predict(X: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]) scipy.sparse._csr.csr_matrix

Predicts scores, given the interactions in X

Recommends items for each nonzero user in the X matrix.

This function is a wrapper around the _predict() method, and performs checks on in- and output data to guarantee proper computation.

Parameters

X (Matrix) – interactions to predict from.

Returns

The recommendation scores in a sparse matrix format.

Return type

csr_matrix

set_params(**params)

Set the parameters of the estimator.

Parameters

params (dict) – Estimator parameters