swap.users.and.items: Swap users and items in the model

View source: R/other.R

swap.users.and.itemsR Documentation

Swap users and items in the model


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".


swap.users.and.items(model, precompute = TRUE)



A collective matrix factorization model from this package - see fit_models for details.


Whether to calculate the precomputed matrices for speeding up predictions in new data.


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.



### Generate a small random matrix
n_users <- 10L
n_items <- 8L
k <- 3L
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)

cmfrec documentation built on April 11, 2023, 6 p.m.