swap.users.and.items | R Documentation |

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)

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

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.

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)

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.