recpack.algorithms.TARSItemKNNXia
- class recpack.algorithms.TARSItemKNNXia(K: int = 200, fit_decay: float = 0.5, decay_function: str = 'convex', decay_interval: int = 86400)
Time aware variant of ItemKNN that considers the time between two interactions when computing similarity between two items.
First described in C. Xia, X. Jiang, Sen Liu, Zhaobo Luo and Zhang Yu, “Dynamic item-based recommendation algorithm with time decay,” 2010 Sixth International Conference on Natural Computation, Yantai, 2010, pp. 242-247, doi: 10.1109/ICNC.2010.5582899.
For each item the K most similar items are computed during fit. The
decay_function
parameter decides how to compute the similarity between two items.\[\text{sim}(i,j) = \sum\limits_{u \in U} R_{u,i} \cdot R_{u,j} \cdot \theta(|T_{u,i} - T_{u,j}|)\]Supported options are:
"concave"
,"convex"
and"linear"
.Concave decay function between item i and j is computed as:
\[\theta(x) = \alpha^{x}, \alpha \in [0, 1]\]Convex decay function between item i and j is computed as:
\[\theta(x) = 1 - \beta^{t-x}, \beta \in (0, 1)\]Linear decay function between item i and j is computed as:
\[\theta(x) = 1 - \frac{x}{t} \cdot \gamma, \gamma \in [0, 1]\]
Where \(t\) is the time between the interactions with both items.
- Parameters
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 0.5.
decay_function (str, optional) – The decay function that needs to be applied on the item similarity scores. Defaults to
"convex"
.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).
Methods
fit
(X)Fit the model to the input interaction matrix.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X)Predicts scores, given the interactions in X
set_params
(**params)Set the parameters of the estimator.
Attributes
DECAY_FUNCTIONS
The supported Decay function options.
SUPPORTED_SIMILARITIES
Supported similarities are
"cooc"
and"conditional_probability"
.Name of the object.
Name of the object's class.
- SUPPORTED_DECAY_FUNCTIONS = ['concave', 'convex', 'linear']
The supported Decay function options.
- 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
- Parameters
X (Matrix) – The interactions to fit the model on.
- Returns
self, fitted algorithm
- Return type
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
routing – A
MetadataRequest
encapsulating routing information.- Return type
MetadataRequest
- get_params(deep=True)
Get parameters for this estimator.
- Parameters
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
params – Parameter names mapped to their values.
- Return type
dict
- 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
- 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