output: Exporting Factorization Matrices

outputR Documentation

Exporting Factorization Matrices

Description

This method is a member function of class "RecoSys" that exports the user score matrix P and the item score matrix Q.

Prior to calling this method, model needs to be trained using member function $train().

The common usage of this method is

r = Reco()
r$train(...)
r$output(out_P = out_file("mat_P.txt"), out_Q = out_file("mat_Q.txt"))

Arguments

r

Object returned by Reco().

out_P

An object of class Output that specifies the output format of the user matrix, typically returned by function out_file(), out_memory() or out_nothing(). out_file() writes the matrix into a file, with each row representing a user and each column representing a latent factor. out_memory() exports the matrix into the return value of $output(). out_nothing() means the matrix will not be exported.

out_Q

Ditto, but for the item matrix.

Value

A list with components P and Q. They will be filled with user or item matrix if out_memory() is used in the function argument, otherwise NULL will be returned.

Author(s)

Yixuan Qiu <https://statr.me>

References

W.-S. Chin, Y. Zhuang, Y.-C. Juan, and C.-J. Lin. A Fast Parallel Stochastic Gradient Method for Matrix Factorization in Shared Memory Systems. ACM TIST, 2015.

W.-S. Chin, Y. Zhuang, Y.-C. Juan, and C.-J. Lin. A Learning-rate Schedule for Stochastic Gradient Methods to Matrix Factorization. PAKDD, 2015.

W.-S. Chin, B.-W. Yuan, M.-Y. Yang, Y. Zhuang, Y.-C. Juan, and C.-J. Lin. LIBMF: A Library for Parallel Matrix Factorization in Shared-memory Systems. Technical report, 2015.

See Also

$train(), $predict()

Examples

train_set = system.file("dat", "smalltrain.txt", package = "recosystem")
r = Reco()
set.seed(123) # This is a randomized algorithm
r$train(data_file(train_set), out_model = file.path(tempdir(), "model.txt"),
        opts = list(dim = 10, nmf = TRUE))

## Write P and Q matrices to files
P_file = out_file(tempfile())
Q_file = out_file(tempfile())
r$output(P_file, Q_file)
head(read.table(P_file@dest, header = FALSE, sep = " "))
head(read.table(Q_file@dest, header = FALSE, sep = " "))

## Skip P and only export Q
r$output(out_nothing(), Q_file)

## Return P and Q in memory
res = r$output(out_memory(), out_memory())
head(res$P)
head(res$Q)


yixuan/recosystem documentation built on May 6, 2023, 9:26 a.m.