# recpack.algorithms.KUNN

class recpack.algorithms.KUNN(Ku: int = 100, Ki: int = 100)

Unified Nearest Neighbour algorithm combining user and item neighbourhood methods.

KUNN Algorithm as described in ‘Unifying Nearest Neighbors Collaborative Filtering’ Verstrepen et al. (10.1145/2645710.2645731)

Computes the item KNN model and stores training interactions at fitting time. Computes the user KNN model between test and training users at prediction time.

Scores are computed as a sum of item and user similarity.

user KNN are computed using

$sim(u,v) = \sum_{i \in I} { \frac{ R_{ui} R_{vi}}{\sqrt{c(u) c(v) c(i)}}}$

item KNN are computed as

$sim(i,j) = \sum_{u \in U} { \frac{ R_{ui} R_{vi}}{\sqrt{c(i) c(u) c(j)}}}$

Similarity is computed as

$sim(u, i) = S_U(u, i) + S_I(u, i)$

Where user similarity is computed as

$S_U(u, i) = \sum_{v \in KNN(u)} \frac{R_{vi} * sim(u,v)}{\sqrt{c(i)}}$

and item similarity is computed as

$S_I(u, i) = \sum_{j \in KNN(i)} \frac{R_{uj} * sim(i, j)}{\sqrt{c(u)}}$
Parameters
• Ku (int, optional) – How many neighbours to keep in the user similarity matrix. Defaults to 100.

• Ki (int, optional) – How many items to keep as neighbours in the item similarity matrix. Defaults to 100.

Methods

 Fit the model to the input interaction matrix. get_params([deep]) Get parameters for this estimator. 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])

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_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