Skip to content

Instantly share code, notes, and snippets.

@tachi-hi
Created May 24, 2012 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tachi-hi/2779734 to your computer and use it in GitHub Desktop.
Save tachi-hi/2779734 to your computer and use it in GitHub Desktop.
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