def test_sparse_input(): """Test that sparse matrices are accepted as input""" from scipy.sparse import csr_matrix A = np.abs(random_state.randn(10, 10)) A[:, 2 * np.arange(5)] = 0 T1 = nmf.ProjectedGradientNMF(n_components=5, init='random', random_state=999).fit_transform(A) A_sparse = csr_matrix(A) pg_nmf = nmf.ProjectedGradientNMF(n_components=5, init='random', random_state=999) T2 = pg_nmf.fit_transform(A_sparse) assert_array_almost_equal( pg_nmf.reconstruction_err_, linalg.norm(A - np.dot(T2, pg_nmf.components_), 'fro')) assert_array_almost_equal(T1, T2) # same with sparseness T2 = nmf.ProjectedGradientNMF(n_components=5, init='random', sparseness='data', random_state=999).fit_transform(A_sparse) T1 = nmf.ProjectedGradientNMF(n_components=5, init='random', sparseness='data', random_state=999).fit_transform(A)
def test_projgrad_nmf_sparseness(): # Test sparseness # Test that sparsity constraints actually increase sparseness in the # part where they are applied. A = np.abs(random_state.randn(10, 10)) m = nmf.ProjectedGradientNMF(n_components=5, random_state=0).fit(A) data_sp = nmf.ProjectedGradientNMF(n_components=5, sparseness='data', random_state=0).fit(A).data_sparseness_ comp_sp = nmf.ProjectedGradientNMF(n_components=5, sparseness='components', random_state=0).fit(A).comp_sparseness_ assert_greater(data_sp, m.data_sparseness_) assert_greater(comp_sp, m.comp_sparseness_)
def test_sparse_input(): """Test that sparse matrices are accepted as input""" from scipy.sparse import csr_matrix A = np.abs(random_state.randn(10, 10)) A[:, 2 * np.arange(5)] = 0 T1 = nmf.ProjectedGradientNMF(n_components=5, init='random', random_state=999).fit_transform(A) A = csr_matrix(A) T2 = nmf.ProjectedGradientNMF(n_components=5, init='random', random_state=999).fit_transform(A) assert_array_almost_equal(T1, T2)
def test_projgrad_nmf_fit_nn_output(): """Test that the decomposition does not contain negative values""" A = np.c_[5 * np.ones(5) - xrange(1, 6), 5 * np.ones(5) + xrange(1, 6)] for init in (None, 'nndsvd', 'nndsvda', 'nndsvdar'): model = nmf.ProjectedGradientNMF(n_components=2, init=init) transf = model.fit_transform(A) assert_false((model.components_ < 0).any() or (transf < 0).any())
def test_projgrad_nmf_transform(): # Test that NMF.transform returns close values # (transform uses scipy.optimize.nnls for now) A = np.abs(random_state.randn(6, 5)) m = nmf.ProjectedGradientNMF(n_components=5, init='nndsvd', random_state=0) transf = m.fit_transform(A) assert_true(np.allclose(transf, m.transform(A), atol=1e-2, rtol=0))
def test_projgrad_nmf_fit_close(): """Test that the fit is not too far away""" assert_true( nmf.ProjectedGradientNMF(5, init='nndsvda').fit( np.abs(random_state.randn(6, 5))).reconstruction_err_ < 0.05)
def test_projgrad_nmf_fit_nn_input(): """Test model fit behaviour on negative input""" A = -np.ones((2, 2)) m = nmf.ProjectedGradientNMF(n_components=2, init=None) m.fit(A)
def test_n_components_greater_n_features(): """Smoke test for the case of more components than features.""" A = np.abs(random_state.randn(30, 10)) nmf.ProjectedGradientNMF(n_components=15, sparseness='data').fit(A)
def test_projgrad_nmf_fit_close(): """Test that the fit is not too far away""" pnmf = nmf.ProjectedGradientNMF(5, init='nndsvda', random_state=0) X = np.abs(random_state.randn(6, 5)) assert_less(pnmf.fit(X).reconstruction_err_, 0.05)
def test_projgrad_nmf_fit_nn_input(): # Test model fit behaviour on negative input A = -np.ones((2, 2)) m = nmf.ProjectedGradientNMF(n_components=2, init=None, random_state=0) m.fit(A)