@@ -42,25 +42,28 @@ def test_initialize_nn_output():
42
42
def test_parameter_checking ():
43
43
A = np .ones ((2 , 2 ))
44
44
name = 'spam'
45
+ # FIXME : should be removed in 0.26
46
+ init = 'nndsvda'
45
47
msg = "Invalid solver parameter: got 'spam' instead of one of"
46
- assert_raise_message (ValueError , msg , NMF (solver = name ).fit , A )
48
+ assert_raise_message (ValueError , msg , NMF (solver = name , init = init ).fit , A )
47
49
msg = "Invalid init parameter: got 'spam' instead of one of"
48
50
assert_raise_message (ValueError , msg , NMF (init = name ).fit , A )
49
51
msg = "Invalid regularization parameter: got 'spam' instead of one of"
50
- assert_raise_message (ValueError , msg , NMF (regularization = name ).fit , A )
52
+ assert_raise_message (ValueError , msg , NMF (regularization = name ,
53
+ init = init ).fit , A )
51
54
msg = "Invalid beta_loss parameter: got 'spam' instead of one"
52
- assert_raise_message (ValueError , msg , NMF (solver = 'mu' ,
55
+ assert_raise_message (ValueError , msg , NMF (solver = 'mu' , init = init ,
53
56
beta_loss = name ).fit , A )
54
57
msg = "Invalid beta_loss parameter: solver 'cd' does not handle "
55
58
msg += "beta_loss = 1.0"
56
- assert_raise_message (ValueError , msg , NMF (solver = 'cd' ,
59
+ assert_raise_message (ValueError , msg , NMF (solver = 'cd' , init = init ,
57
60
beta_loss = 1.0 ).fit , A )
58
61
59
62
msg = "Negative values in data passed to"
60
- assert_raise_message (ValueError , msg , NMF ().fit , - A )
63
+ assert_raise_message (ValueError , msg , NMF (init = init ).fit , - A )
61
64
assert_raise_message (ValueError , msg , nmf ._initialize_nmf , - A ,
62
65
2 , 'nndsvd' )
63
- clf = NMF (2 , tol = 0.1 ).fit (A )
66
+ clf = NMF (2 , tol = 0.1 , init = init ).fit (A )
64
67
assert_raise_message (ValueError , msg , clf .transform , - A )
65
68
66
69
for init in ['nndsvd' , 'nndsvda' , 'nndsvdar' ]:
@@ -176,7 +179,9 @@ def test_n_components_greater_n_features():
176
179
# Smoke test for the case of more components than features.
177
180
rng = np .random .mtrand .RandomState (42 )
178
181
A = np .abs (rng .randn (30 , 10 ))
179
- NMF (n_components = 15 , random_state = 0 , tol = 1e-2 ).fit (A )
182
+ # FIXME : should be removed in 0.26
183
+ init = 'random'
184
+ NMF (n_components = 15 , random_state = 0 , tol = 1e-2 , init = init ).fit (A )
180
185
181
186
182
187
@pytest .mark .parametrize ('solver' , ['cd' , 'mu' ])
@@ -214,7 +219,7 @@ def test_nmf_sparse_transform():
214
219
215
220
for solver in ('cd' , 'mu' ):
216
221
model = NMF (solver = solver , random_state = 0 , n_components = 2 ,
217
- max_iter = 400 )
222
+ max_iter = 400 , init = 'nndsvd' )
218
223
A_fit_tr = model .fit_transform (A )
219
224
A_tr = model .transform (A )
220
225
assert_array_almost_equal (A_fit_tr , A_tr , decimal = 1 )
@@ -436,13 +441,17 @@ def test_nmf_regularization():
436
441
rng = np .random .mtrand .RandomState (42 )
437
442
X = np .abs (rng .randn (n_samples , n_features ))
438
443
444
+ # FIXME : should be removed in 0.26
445
+ init = 'nndsvda'
439
446
# L1 regularization should increase the number of zeros
440
447
l1_ratio = 1.
441
448
for solver in ['cd' , 'mu' ]:
442
449
regul = nmf .NMF (n_components = n_components , solver = solver ,
443
- alpha = 0.5 , l1_ratio = l1_ratio , random_state = 42 )
450
+ alpha = 0.5 , l1_ratio = l1_ratio , random_state = 42 ,
451
+ init = init )
444
452
model = nmf .NMF (n_components = n_components , solver = solver ,
445
- alpha = 0. , l1_ratio = l1_ratio , random_state = 42 )
453
+ alpha = 0. , l1_ratio = l1_ratio , random_state = 42 ,
454
+ init = init )
446
455
447
456
W_regul = regul .fit_transform (X )
448
457
W_model = model .fit_transform (X )
@@ -462,18 +471,20 @@ def test_nmf_regularization():
462
471
l1_ratio = 0.
463
472
for solver in ['cd' , 'mu' ]:
464
473
regul = nmf .NMF (n_components = n_components , solver = solver ,
465
- alpha = 0.5 , l1_ratio = l1_ratio , random_state = 42 )
474
+ alpha = 0.5 , l1_ratio = l1_ratio , random_state = 42 ,
475
+ init = init )
466
476
model = nmf .NMF (n_components = n_components , solver = solver ,
467
- alpha = 0. , l1_ratio = l1_ratio , random_state = 42 )
477
+ alpha = 0. , l1_ratio = l1_ratio , random_state = 42 ,
478
+ init = init )
468
479
469
480
W_regul = regul .fit_transform (X )
470
481
W_model = model .fit_transform (X )
471
482
472
483
H_regul = regul .components_
473
484
H_model = model .components_
474
485
475
- assert W_model . mean () > W_regul . mean ()
476
- assert H_model . mean () > H_regul . mean ()
486
+ assert ( linalg . norm ( W_model )) ** 2. + ( linalg . norm ( H_model )) ** 2. > \
487
+ ( linalg . norm ( W_regul )) ** 2. + ( linalg . norm ( H_regul )) ** 2.
477
488
478
489
479
490
@ignore_warnings (category = ConvergenceWarning )
@@ -541,7 +552,9 @@ def test_nmf_dtype_match(dtype_in, dtype_out, solver, regularization):
541
552
# Check that NMF preserves dtype (float32 and float64)
542
553
X = np .random .RandomState (0 ).randn (20 , 15 ).astype (dtype_in , copy = False )
543
554
np .abs (X , out = X )
544
- nmf = NMF (solver = solver , regularization = regularization )
555
+ # FIXME : should be removed in 0.26
556
+ init = 'nndsvda'
557
+ nmf = NMF (solver = solver , regularization = regularization , init = init )
545
558
546
559
assert nmf .fit (X ).transform (X ).dtype == dtype_out
547
560
assert nmf .fit_transform (X ).dtype == dtype_out
@@ -555,9 +568,13 @@ def test_nmf_float32_float64_consistency(solver, regularization):
555
568
# Check that the result of NMF is the same between float32 and float64
556
569
X = np .random .RandomState (0 ).randn (50 , 7 )
557
570
np .abs (X , out = X )
558
- nmf32 = NMF (solver = solver , regularization = regularization , random_state = 0 )
571
+ # FIXME : should be removed in 0.26
572
+ init = 'nndsvda'
573
+ nmf32 = NMF (solver = solver , regularization = regularization , random_state = 0 ,
574
+ init = init )
559
575
W32 = nmf32 .f
1241
it_transform (X .astype (np .float32 ))
560
- nmf64 = NMF (solver = solver , regularization = regularization , random_state = 0 )
576
+ nmf64 = NMF (solver = solver , regularization = regularization , random_state = 0 ,
577
+ init = init )
561
578
W64 = nmf64 .fit_transform (X )
562
579
563
580
assert_allclose (W32 , W64 , rtol = 1e-6 , atol = 1e-5 )
@@ -576,3 +593,20 @@ def test_nmf_custom_init_dtype_error():
576
593
577
594
with pytest .raises (TypeError , match = "should have the same dtype as X" ):
578
595
non_negative_factorization (X , H = H , update_H = False )
596
+
597
+
598
+ # FIXME : should be removed in 0.26
599
+ def test_init_default_deprecation ():
600
+ # Test FutureWarning on init default
601
+ msg = ("The 'init' value, when 'init=None' and "
602
+ "n_components is less than n_samples and "
603
+ "n_features, will be changed from 'nndsvd' to "
604
+ "'nndsvda' in 0.26." )
605
+ rng = np .random .mtrand .RandomState (42 )
606
+ A = np .abs (rng .randn (6 , 5 ))
607
+ with pytest .warns (FutureWarning , match = msg ):
608
+ nmf ._initialize_nmf (A , 3 )
609
+ with pytest .warns (FutureWarning , match = msg ):
610
+ NMF ().fit (A )
611
+ with pytest .warns (FutureWarning , match = msg ):
612
+ non_negative_factorization (A )
0 commit comments