def test_minibatch_nmf_verbose(): # Check verbose mode of MiniBatchNMF for better coverage. A = np.random.RandomState(0).random_sample((100, 10)) nmf = MiniBatchNMF(tol=1e-2, random_state=0, verbose=1) old_stdout = sys.stdout sys.stdout = StringIO() try: nmf.fit(A) finally: sys.stdout = old_stdout
def test_minibatch_nmf_negative_beta_loss(beta_loss): """Check that an error is raised if beta_loss < 0 and X contains zeros.""" rng = np.random.RandomState(0) X = rng.normal(size=(6, 5)) X[X < 0] = 0 nmf = MiniBatchNMF(beta_loss=beta_loss, random_state=0) msg = "When beta_loss <= 0 and X contains zeros, the solver may diverge." with pytest.raises(ValueError, match=msg): nmf.fit(X)
def test_nmf_dtype_match(dtype_in, dtype_out, solver): # Check that NMF preserves dtype (float32 and float64) X = np.random.RandomState(0).randn(20, 15).astype(dtype_in, copy=False) np.abs(X, out=X) nmf = NMF(solver=solver) assert nmf.fit(X).transform(X).dtype == dtype_out assert nmf.fit_transform(X).dtype == dtype_out assert nmf.components_.dtype == dtype_out
def test_nmf_dtype_match(Estimator, solver, dtype_in, dtype_out): # Check that NMF preserves dtype (float32 and float64) X = np.random.RandomState(0).randn(20, 15).astype(dtype_in, copy=False) np.abs(X, out=X) nmf = Estimator(alpha_W=1.0, alpha_H=1.0, tol=1e-2, random_state=0, **solver) assert nmf.fit(X).transform(X).dtype == dtype_out assert nmf.fit_transform(X).dtype == dtype_out assert nmf.components_.dtype == dtype_out
def test_nmf_dtype_match(dtype_in, dtype_out, solver, regularization): # Check that NMF preserves dtype (float32 and float64) X = np.random.RandomState(0).randn(20, 15).astype(dtype_in, copy=False) np.abs(X, out=X) # FIXME : should be removed in 1.1 init = 'nndsvda' nmf = NMF(solver=solver, regularization=regularization, init=init) assert nmf.fit(X).transform(X).dtype == dtype_out assert nmf.fit_transform(X).dtype == dtype_out assert nmf.components_.dtype == dtype_out