class recpack.algorithms.TARSItemKNN(K: int = 200, fit_decay: float = 1.1574074074074073e-05, predict_decay: float = 1.1574074074074073e-05, decay_interval: int = 1, similarity: str = 'cosine', decay_function: str = 'exponential')

Framework for time aware variants of the ItemKNN algorithm.

This class was inspired by works from Liu, Nathan N., et al. (2010), Ding et al. (2005) and Lee et al. (2007).

The framework for these approaches can be summarised as:

  • When training the user interaction matrix is weighted to take into account temporal information.

  • Similarities are computed on this weighted matrix, using various similarity measures.

  • When predicting the interactions are similarly weighted, giving more weight to more recent interactions.

  • Recommendation scores are obtained by multiplying the weighted interaction matrix with the previously computed similarity matrix.

The similarity between items is based on their decayed interaction vectors:

\[\text{sim}(i,j) = s(\Gamma(A_i), \Gamma(A_j))\]

Where \(s\) is a similarity function (like cosine), \(\Gamma\) a decay function (like exponential_decay) and \(A_i\) contains the distances to now from when the users interacted with item i, if they interacted with the item at all (else the value is 0).

During computation, ‘now’ is considered as the maximal timestamp in the matrix + 1. As such the age is always a positive non-zero value.

  • K (int, Optional) – How many neigbours to use per item, make sure to pick a value below the number of columns of the matrix to fit on. Defaults to 200

  • fit_decay (float, optional) – Defines the decay scaling used for decay during model fitting. Defaults to `` 1 / (24 * 3600)`` (one day).

  • predict_decay (float, optional) – Defines the decay scaling used for decay during prediction. Defaults to 1 / (24 * 3600) (one day).

  • decay_interval (int, optional) – Size of a single time unit in seconds. Allows more finegrained parameters for large scale datasets where events are collected over months of data. Defaults to 1 (second).

  • similarity (str, Optional) – Which similarity measure to use. Defaults to "cosine". ["cosine", "conditional_probability", "pearson"] are supported.

  • decay_function – The decay function to use, defaults to "exponential". Supported values are ["exponential", "log", "linear", "concave", "convex", "inverse"]



Fit the model to the input interaction matrix.


Get parameters for this estimator.


Predicts scores, given the interactions in X


Set the parameters of the estimator.





Name of the object.


Name of the object's class.

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,

  • The fit function gets timed, and this will get printed

  • Input data is converted to expected type using call to _transform_predict_input()

  • The model is trained using the _fit() method

  • _check_fit_complete() is called to check fitting was succesful


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


self, fitted algorithm

Return type



Get parameters for this estimator.


deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.


params – Parameter names mapped to their values.

Return type


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.

  • Checks that model is fitted correctly

  • checks the output using _check_prediction() function


X (Matrix) – interactions to predict from.


The recommendation scores in a sparse matrix format.

Return type



Set the parameters of the estimator.


params (dict) – Estimator parameters