swap.users.and.items {cmfrec} | R Documentation |
Swap users and items in the model
Description
This function will swap the users and items in a given matrix factorization model. Since the package functionality is user-centric, it is generally not possible or not efficient to make predictions about items (e.g. recommend users for a given item or calculate new item factors).
This function allows using the same API while avoiding model refitting or deep copies of data by simply swapping the matrices, IDs, and hyperparameters as necessary.
The resulting object can be used with the same functions as the original, such as topN or factors, but any mention of "user" in the functions will now mean "items".
Usage
swap.users.and.items(model, precompute = TRUE)
Arguments
model |
A collective matrix factorization model from this package - see fit_models for details. |
precompute |
Whether to calculate the precomputed matrices for speeding up predictions in new data. |
Value
The same model object as before, but with the internal data swapped in order to make predictions about items. If passing 'precompute=TRUE', it will also generate precomputed matrices which can be used to speed up predictions.
Examples
library(cmfrec)
### Generate a small random matrix
n_users <- 10L
n_items <- 8L
k <- 3L
set.seed(1)
X <- matrix(rnorm(n_users*n_items), nrow=n_users)
### Factorize it
model <- CMF(X, k=k, verbose=FALSE, nthreads=1L)
### Now swap the users and items
model.swapped <- swap.users.and.items(model)
### These will now throw the same result
### (up to numerical precision)
item_factors(model, X[, 1])
factors_single(model.swapped, X[, 1])
### Swapping it again restores the original
model.restored <- swap.users.and.items(model.swapped)
### These will throw the same result
topN(model, user=2, n=3)
topN(model.restored, user=2, n=3)