zdebruine / RcppML

Rcpp Machine Learning: Fast robust NMF, divisive clustering, and more
GNU General Public License v2.0
89 stars 15 forks source link

how to check the top features in each rank? #27

Closed ruiye88 closed 2 years ago

ruiye88 commented 2 years ago

Hi Zach,

Thank you for implementing this great package for fast NMF. Just wondering is there a similar function like the extracFeatures from the original nmf package that allows us to quickly get the top fetures from each rank? Thanks!

Best, Rui

zdebruine commented 2 years ago

Hi Rui,

You can do this using some basic R functions. Here's a working example if you're using the development version:

library(RcppML)
data(hawaiibirds)
nmf_model <- nmf(hawaiibirds$counts, k = 10)

# split the "w" matrix into a list of column vectors
w <- lapply(seq_len(ncol(nmf_model@w)), function(i) nmf_model@w[,i])

# get the top "n" values in each list item
lapply(h, function(x) head(sort(x, decreasing = TRUE), n = 10L))

This is quite well vectorised and should run efficiently. You can also use dplyr and friends to a similar effect.

You may also be able to convert an RcppML::nmf class to an NMF class if you so desire, but my experience is that the NMF package is a bit out of date and does not outperform base R and tidyverse functionality.

Hope this helps! Let me know if you have any other questions!

Zach

ruiye88 commented 2 years ago

Hi Zach,

Tkank you for the quick reply!!

Best, Rui

Hi Rui,

You can do this using some basic R functions. Here's a working example if you're using the development version:

library(RcppML)
data(hawaiibirds)
nmf_model <- nmf(hawaiibirds$counts, k = 10)

# split the "w" matrix into a list of column vectors
w <- lapply(seq_len(ncol(nmf_model@w)), function(i) nmf_model@w[,i])

# get the top "n" values in each list item
lapply(h, function(x) head(sort(x, decreasing = TRUE), n = 10L))

This is quite well vectorised and should run efficiently. You can also use dplyr and friends to a similar effect.

You may also be able to convert an RcppML::nmf class to an NMF class if you so desire, but my experience is that the NMF package is a bit out of date and does not outperform base R and tidyverse functionality.

Hope this helps! Let me know if you have any other questions!

Zach