def test_svd_ncomponents_lt_nfeatures(): # smoke test for input where a dimension is 1 rng = np.random.mtrand.RandomState(42) model = CMF(n_components=3, solver="newton", x_init='svd', y_init='svd', U_non_negative=False, V_non_negative=False, Z_non_negative=False, random_state=0, max_iter=1) X = rng.randn(6, 4) Y = rng.randn(4, 2) model.fit(X, Y)
def test_fit_close(solver): rng = np.random.mtrand.RandomState(42) # Test that the fit is not too far away for rndm_state in [0]: pnmf = CMF(n_components=5, solver=solver, x_init='nndsvdar', y_init='nndsvdar', random_state=rndm_state, max_iter=1000) X = np.abs(rng.randn(6, 5)) Y = np.abs(rng.randn(5, 6)) assert_less(pnmf.fit(X, Y).reconstruction_err_, 0.1)
def test_transform_after_fit(solver): rng = np.random.mtrand.RandomState(36) X = rng.randn(7, 5) Y = rng.randn(5, 3) fit_model = CMF(n_components=2, solver=solver, x_init='random', y_init='random', U_non_negative=False, V_non_negative=False, Z_non_negative=False, random_state=0, max_iter=100) fit_transform_model = clone(fit_model) fit_model.fit(X, Y) U_f, V_f, Z_f = fit_model.transform(X, Y) U_ft, V_ft, Z_ft = fit_transform_model.fit_transform(X, Y) # the initalizations will differ, so the results may also differ slightly assert_array_almost_equal(U_f, U_ft, decimal=2) assert_array_almost_equal(V_f, V_ft, decimal=2) assert_array_almost_equal(Z_f, Z_ft, decimal=2)
def test_stochastic_newton_solver(): rng = np.random.mtrand.RandomState(42) model = CMF(n_components=5, solver="newton", x_init='svd', y_init='svd', U_non_negative=False, V_non_negative=False, Z_non_negative=False, alpha=0.5, sg_sample_ratio=0.5, random_state=0, max_iter=1000) X = rng.randn(6, 5) Y = rng.randn(5, 6) assert_less(model.fit(X, Y).reconstruction_err_, 0.1)
def test_recover_low_rank_matrix(solver): rng = np.random.mtrand.RandomState(42) # Test that the fit is not too far away pnmf = CMF(5, solver=solver, x_init='nndsvdar', y_init='nndsvdar', random_state=0, max_iter=1000) U = np.abs(rng.randn(10, 5)) V = np.abs(rng.randn(8, 5)) Z = np.abs(rng.randn(6, 5)) X = np.dot(U, V.T) Y = np.dot(V, Z.T) assert_less(pnmf.fit(X, Y).reconstruction_err_, 1.0)
def test_stochastic_newton_solver_sparse_input_close(): rng = np.random.mtrand.RandomState(42) model = CMF(n_components=5, solver="newton", x_init='svd', y_init='svd', U_non_negative=False, V_non_negative=False, Z_non_negative=False, alpha=0.5, sg_sample_ratio=0.5, random_state=0, max_iter=1000) A = rng.randn(6, 5) B = rng.randn(5, 6) A_sparse = csr_matrix(A) B_sparse = csr_matrix(B) assert_less(model.fit(A_sparse, B_sparse).reconstruction_err_, 0.1)