recpack.algorithms.STAN

class recpack.algorithms.STAN(K: int = 200, interaction_decay: float = 0.0002777777777777778, session_decay: float = 0.0002777777777777778, distance_from_match_decay: float = 1)

Sequence and Time Aware Neighbourhoods algorithm.

Algorithm presented by Garg, Diksha, et al. “Sequence and time aware neighborhood for session-based recommendations: STAN.”

The algorithm is a modified version of UserKNN with several decay schemes applied.

Each of the user’s interactions are weighted by multiplying them with

\[e^{- \lambda_1 \, (t_{max} - t_i)}\]

Where lambda_1 is the interaction_decay parameter.

A second weighting scheme is applied when computing session similarities. The time of a session is the last timestamp in that session.

Session similarities are weighted by multiplication with

\[e^{- \lambda_2 \, |T_{s1} - T_{s_2}|}\]

Where lambda_2 is the session_decay parameter.

A final weighting is applied to recommend items closest to the last matching item between similar users. For each item in a neighbours history, the session similarity is weighted additionally by

\[e^{- \lambda_3 \, |pos_i - pos_{matching}|}\]

Where lambda_3 is the distance_from_match_decay parameter.

Note

We modified the decay computations from the paper, by using a multiplicative weight, rather than a division. This allows us to easily disable a weight. Typical values for decay parameters will be between 0 and 1.

Parameters
  • K (int, optional) – The amount of sessions to be considered as neighbourhood, defaults to 200

  • interaction_decay (float, optional) – The decay factor for session history weighting, defaults to 1/3600

  • session_decay (float, optional) – The decay factor for session similarity computation, defaults to 1/3600

  • distance_from_match_decay (float, optional) – The decay factor for prediction item weighting, defaults to 1

Methods

fit(X)

Fit the model to the input interaction matrix.

get_metadata_routing()

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

identifier

Name of the object.

name

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

Parameters

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

Returns

self, fitted algorithm

Return type

Algorithm

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