Implements regularized non-negative matrix factorization by a method similar to Lee & Seung, “Algorithms for Non-negative Matrix Factorization,” 2001.
– Steven E. Pav, shabbychef@gmail.com
This package may be installed from CRAN; the latest version may be found on github via devtools, or installed via drat:
# CRAN
install.packages(c("rnnmf"))
# devtools
if (require(devtools)) {
# latest greatest
install_github("shabbychef/rnnmf")
}# via drat:
if (require(drat)) {
:::add("shabbychef")
drat# not yet: install.packages('rnnmf')
}
Non-negative matrix factorization is a tool for decomposing a non-negative matrix \(Y\) approximately as \(Y \approx L R\) for non-negative matrices \(L, R\) of pre-specified rank. This package provides code for non-negative matrix factorization with penalty terms for the \(\ell_1\) and \(\ell_2\) norms of the two factors, as well as for non-orthogonality of the factors. The code is based on the conceptually simple multiplicative update of Lee & Seung. An additive update based on the same ideas is also given.
This code is provided mostly for research purposes, and no warranty is given regarding speed, or convergence.
We demonstrate the usage of the multiplicative and additive updates in factoring a small matrix which we constructed to be the product of two reduced rank non-negative matrices.
library(dplyr)
library(rnnmf)
library(ggplot2)
<- function(Y, L, R) {
frobenius_norm_err sqrt(sum(abs(Y - L %*% R)^2))
}<- function(nr, nc, ...) {
runifmat matrix(pmax(0, runif(nr * nc, ...)), nrow = nr)
}<- function(Y_t, L_0, R_0, niter = 10000L) {
test_a_bunch <- new.env()
iter_hist "history"]] <- rep(NA_real_, niter)
iter_hist[[
<- function(iteration, Y, L, R,
on_iteration_end
...) {"history"]][iteration] <<- frobenius_norm_err(Y,
iter_hist[[
L, R)
}<- aurnmf(Y_t, L_0, R_0, max_iterations = niter,
wuz on_iteration_end = on_iteration_end)
<- tibble(x = seq_along(iter_hist[["history"]]),
df1 y = iter_hist[["history"]]) %>%
mutate(method = "additive, optimal step")
"history"]] <- rep(NA_real_, niter)
iter_hist[[<- murnmf(Y_t, L_0, R_0, max_iterations = niter,
wuz on_iteration_end = on_iteration_end)
<- tibble(x = seq_along(iter_hist[["history"]]),
df2 y = iter_hist[["history"]]) %>%
mutate(method = "multiplicative")
<- bind_rows(df1, df2) %>%
retv mutate(nr = nrow(Y_t), nc = ncol(Y_t), nd = ncol(L_0),
max_iter = niter)
return(retv)
}
<- 30
nr <- 8
nc <- 3
nd set.seed(1234)
<- runifmat(nr, nd)
L_t <- runifmat(nd, nc)
R_t <- L_t %*% R_t
Y_t
<- runifmat(nrow(Y_t), nd + 1)
L_0 <- runifmat(ncol(L_0), ncol(Y_t))
R_0
test_a_bunch(Y_t, L_0, R_0, niter = 10000L) %>%
ggplot(aes(x, y, color = method)) + geom_line() +
scale_x_log10(labels = scales::comma) + scale_y_log10() +
labs(x = "Step", y = expression(L[2] ~ ~Error),
title = "Frobenius Norm of Error vs Step",
color = "Method", caption = paste0("Factoring ",
" x ", nc, " matrix down to ", nd,
nr, " dimensions."))