Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Non-negative Matrix Factorization
# Given matrix V, and decompose it as
# V ~ W %*% H
NMF.EUC <- function(V, r, n){
W <- matrix(runif(nrow(V)*r), nrow(V), r)
H <- matrix(runif(ncol(V)*r), r, ncol(V))
d <- c(0)
for(i in 1:n){
W <- W * (V %*% t(H)) / (W %*% H %*% t(H))
H <- H * (t(W) %*% V) / (t(W) %*% W %*% H)
d[i] <- sum(abs(V - W %*% H)**2)
}
return (list(W = W, H = H, d = d))
}
#####################
V <- matrix(runif(1500),30,50)
tmp <- NMF.EUC(V,10,100)
x11(); plot(tmp$d)
x11(); image( t(V) )
x11(); image( t(tmp$W %*% tmp$H) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment