def test_non_negative_factorization_consistency(): # Test that the function is called in the same way, either directly # or through the NMF class rng = np.random.mtrand.RandomState(42) A = np.abs(rng.randn(10, 10)) A[:, 2 * np.arange(5)] = 0 for init in ['random', 'nndsvd']: for solver in ('cd', 'mu'): W_nmf, H, _ = non_negative_factorization(A, init=init, solver=solver, random_state=1, tol=1e-2) W_nmf_2, _, _ = non_negative_factorization(A, H=H, update_H=False, init=init, solver=solver, random_state=1, tol=1e-2) model_class = NMF(init=init, solver=solver, random_state=1, tol=1e-2) W_cls = model_class.fit_transform(A) W_cls_2 = model_class.transform(A) assert_array_almost_equal(W_nmf, W_cls, decimal=10) assert_array_almost_equal(W_nmf_2, W_cls_2, decimal=10)
def test_nmf_sparse_input(): # Test that sparse matrices are accepted as input from scipy.sparse import csc_matrix rng = np.random.mtrand.RandomState(42) A = np.abs(rng.randn(10, 10)) A[:, 2 * np.arange(5)] = 0 A_sparse = csc_matrix(A) for solver in ('cd', 'mu'): for init in ('random', 'nndsvdar'): est1 = NMF(solver=solver, n_components=5, init=init, random_state=0, tol=1e-2) est2 = clone(est1) W1 = est1.fit_transform(A) W2 = est2.fit_transform(A_sparse) H1 = est1.components_ H2 = est2.components_ assert_array_almost_equal(W1, W2) assert_array_almost_equal(H1, H2)
def test_nmf_fit_nn_output(): # Test that the decomposition does not contain negative values A = np.c_[5. - np.arange(1, 6), 5. + np.arange(1, 6)] for solver in ('cd', 'mu'): for init in (None, 'nndsvd', 'nndsvda', 'nndsvdar', 'random'): model = NMF(n_components=2, solver=solver, init=init, random_state=0) transf = model.fit_transform(A) assert not ((model.components_ < 0).any() or (transf < 0).any())
def test_nmf_inverse_transform(solver): # Test that NMF.inverse_transform returns close values random_state = np.random.RandomState(0) A = np.abs(random_state.randn(6, 4)) m = NMF(solver=solver, n_components=4, init='random', random_state=0, max_iter=1000) ft = m.fit_transform(A) A_new = m.inverse_transform(ft) assert_array_almost_equal(A, A_new, decimal=2)
def test_nmf_transform(solver): # Test that NMF.transform returns close values rng = np.random.mtrand.RandomState(42) A = np.abs(rng.randn(6, 5)) m = NMF(solver=solver, n_components=3, init='random', random_state=0, tol=1e-5) ft = m.fit_transform(A) t = m.transform(A) assert_array_almost_equal(ft, t, decimal=2)
def test_nmf_sparse_transform(): # Test that transform works on sparse data. Issue #2124 rng = np.random.mtrand.RandomState(42) A = np.abs(rng.randn(3, 2)) A[1, 1] = 0 A = csc_matrix(A) for solver in ('cd', 'mu'): model = NMF(solver=solver, random_state=0, n_components=2, max_iter=400) A_fit_tr = model.fit_transform(A) A_tr = model.transform(A) assert_array_almost_equal(A_fit_tr, A_tr, decimal=1)
def test_parameter_checking(): A = np.ones((2, 2)) name = 'spam' msg = "Invalid solver parameter: got 'spam' instead of one of" assert_raise_message(ValueError, msg, NMF(solver=name).fit, A) msg = "Invalid init parameter: got 'spam' instead of one of" assert_raise_message(ValueError, msg, NMF(init=name).fit, A) msg = "Invalid beta_loss parameter: got 'spam' instead of one" assert_raise_message(ValueError, msg, NMF(solver='mu', beta_loss=name).fit, A) msg = "Invalid beta_loss parameter: solver 'cd' does not handle " msg += "beta_loss = 1.0" assert_raise_message(ValueError, msg, NMF(solver='cd', beta_loss=1.0).fit, A) msg = "Negative values in data passed to" assert_raise_message(ValueError, msg, NMF().fit, -A) assert_raise_message(ValueError, msg, nmf._initialize_nmf, -A, 2, 'nndsvd') clf = NMF(2, tol=0.1).fit(A) assert_raise_message(ValueError, msg, clf.transform, -A) for init in ['nndsvd', 'nndsvda', 'nndsvdar']: msg = ("init = '{}' can only be used when " "n_components <= min(n_samples, n_features)".format(init)) assert_raise_message(ValueError, msg, NMF(3, init).fit, A) assert_raise_message(ValueError, msg, nmf._initialize_nmf, A, 3, init)
def test_nmf_transform_custom_init(): # Smoke test that checks if NMF.transform works with custom initialization random_state = np.random.RandomState(0) A = np.abs(random_state.randn(6, 5)) n_components = 4 avg = np.sqrt(A.mean() / n_components) H_init = np.abs(avg * random_state.randn(n_components, 5)) W_init = np.abs(avg * random_state.randn(6, n_components)) m = NMF(solver='cd', n_components=n_components, init='custom', random_state=0) m.fit_transform(A, W=W_init, H=H_init) m.transform(A)
def nmf(A, k=10, iter_num=100, epsilon=0.01, calc_error=True, calc_error_num=10): nmf = NMF() nmf.setup(A, k, iter_num, epsilon, calc_error, calc_error_num) nmf.run() return (nmf.W, nmf.H)
def test_n_components_greater_n_features(): # Smoke test for the case of more components than features. rng = np.random.mtrand.RandomState(42) A = np.abs(rng.randn(30, 10)) NMF(n_components=15, random_state=0, tol=1e-2).fit(A)
def test_nmf_fit_close(solver): rng = np.random.mtrand.RandomState(42) # Test that the fit is not too far away pnmf = NMF(5, solver=solver, init='nndsvdar', random_state=0, max_iter=600) X = np.abs(rng.randn(6, 5)) assert_less(pnmf.fit(X).reconstruction_err_, 0.1)