def create_icm_summary(self, data, ranks, n_runs, alpha_bnmf, beta_bnmf, theta_bnmf, k_bnmf, sigma_bnmf): if len(ranks) == 1: icm = nimfa.Icm(data, seed="random_vcol", alpha=alpha_bnmf, beta=beta_bnmf, theta=theta_bnmf, k=k_bnmf, sigma=sigma_bnmf) summary = icm.estimate_rank(rank_range=ranks, n_run=n_runs, what='all') elif len(ranks) > 1: # Following gives: ValueError: could not broadcast input array from shape (50,3) into shape (50) # alphas = [np.random.randn(self.subset_required[1], rank) for rank in ranks] # betas = [np.random.randn(rank, self.subset_required[0]) for rank in ranks] # icm = nimfa.Icm(data, seed="random_vcol", alpha=alphas, beta=betas, theta=theta_bnmf, k=k_bnmf, sigma=sigma_bnmf) icm = nimfa.Icm(data, seed="random_vcol", theta=theta_bnmf, k=k_bnmf, sigma=sigma_bnmf) summary = icm.estimate_rank(rank_range=ranks, n_run=n_runs, what='all') self.summary = summary return summary
def nmf_library(V, W_init, correct_H): #comparisons with non-negative matrix factorization lsnmf = nimfa.Lsnmf(V, seed=None, rank=3, max_iter=100, H=np.array([0., 0., 0.]).reshape(-1, 1), W=W_init) nmf = nimfa.Nmf(V, seed=None, rank=3, max_iter=100, H=np.array([0., 0., 0.]).reshape(-1, 1), W=W_init) icm = nimfa.Icm(V, seed=None, rank=3, max_iter=100, H=np.array([0., 0., 0.]).reshape(-1, 1), W=W_init) bd = nimfa.Bd(V, seed=None, rank=3, max_iter=100, H=np.array([0., 0., 0.]).reshape(-1, 1), W=W_init) pmf = nimfa.Pmf(V, seed=None, rank=3, max_iter=100, H=np.array([0., 0., 0.]).reshape(-1, 1), W=W_init) #lfnmf = nimfa.Lfnmf(V, seed=None, rank=3, max_iter=100, H = np.array([0.,0.,0.]).reshape(-1,1), W = W_init) lsnmf_fit = lsnmf() nmf_fit = nmf() icm_fit = icm() bd_fit = bd() pmf_fit = pmf() lsnmf_error = mean_absolute_error( correct_H, normalized(np.array(lsnmf.H).reshape(-1, ))) nmf_error = mean_absolute_error(correct_H, normalized(np.array(nmf.H).reshape(-1, ))) icm_error = mean_absolute_error(correct_H, normalized(np.array(icm.H).reshape(-1, ))) bd_error = mean_absolute_error(correct_H, normalized(np.array(bd.H).reshape(-1, ))) pmf_error = mean_absolute_error(correct_H, normalized(np.array(pmf.H).reshape(-1, ))) return [lsnmf_error, nmf_error, icm_error, bd_error, pmf_error]
def run_icm(V): """ Run iterated conditional modes. :param V: Target matrix to estimate. :type V: :class:`numpy.matrix` """ rank = 10 pnrg = np.random.RandomState() icm = nimfa.Icm(V, seed="nndsvd", rank=rank, max_iter=12, iiter=20, alpha=pnrg.randn(V.shape[0], rank), beta=pnrg.randn(rank, V.shape[1]), theta=0., k=0., sigma=1.) fit = icm() print_info(fit)
import numpy as np V = np.array([[1, 2, 3], [4, 5, 6], [6, 7, 8]]) print('Target:\n%s' % V) # Initialization callback function def init_info(model): print('Initialized basis matrix:\n%s' % model.basis()) print('Initialized mixture matrix:\n%s' % model.coef()) # Callback is called after initialization and prior to factorization in each run icm = nimfa.Icm(V, seed='random_c', max_iter=10, rank=3, callback_init=init_info) icm_fit = icm() W = icm_fit.basis() print('Basis matrix:\n%s' % W) print(W) H = icm_fit.coef() print('Mixture matrix:\n%s' % H) sm = icm_fit.summary() print('Rss: %5.3f' % sm['rss']) print('Evar: %5.3f' % sm['evar']) print('Iterations: %d' % sm['n_iter'])
import numpy as np import nimfa V = np.random.rand(40, 100) icm = nimfa.Icm(V, seed="nndsvd", rank=10, max_iter=12, iiter=20, alpha=np.random.randn(V.shape[0], 10), beta=np.random.randn(10, V.shape[1]), theta=0., k=0., sigma=1.) icm_fit = icm()