Skip to content

Instantly share code, notes, and snippets.

@idmn
Created September 22, 2023 14:52
Show Gist options
  • Save idmn/2873016177234b65a7d19c62305964d1 to your computer and use it in GitHub Desktop.
Save idmn/2873016177234b65a7d19c62305964d1 to your computer and use it in GitHub Desktop.
NMF essence
library(NMF)
# random non-negative matrix
v <- rmatrix(20, 10)
# fit an NMF model
x <- nmf(v, 5)
# decomposition components
x@fit@W
x@fit@H
# decomposition is not precise
dec_v <- x@fit@W %*% x@fit@H
hist(abs(v - dec_v))
# predicting clusters
predict(x, 'rows')
# actually means finding the largest value in each W row
apply(x@fit@W, 1, which.max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment