@@ -487,6 +487,7 @@ def test_init_mahalanobis(estimator, build_dataset):
487
487
"""
488
488
input_data , labels , _ , X = build_dataset ()
489
489
model = clone (estimator )
490
+ set_random_state (model )
490
491
rng = np .random .RandomState (42 )
491
492
492
493
# Start learning from scratch
@@ -535,3 +536,34 @@ def test_init_mahalanobis(estimator, build_dataset):
535
536
with pytest .raises (ValueError ) as raised_error :
536
537
model .fit (input_data , labels )
537
538
assert str (raised_error .value ) == msg
539
+
540
+
541
+ @pytest .mark .parametrize ('estimator, build_dataset' ,
542
+ [(ml , bd ) for idml , (ml , bd )
543
+ in zip (ids_metric_learners ,
544
+ metric_learners )
545
+ if not hasattr (ml , 'num_dims' ) and
546
+ hasattr (ml , 'init' )],
547
+ ids = [idml for idml , (ml , _ )
548
+ in zip (ids_metric_learners ,
549
+ metric_learners )
550
+ if not hasattr (ml , 'num_dims' ) and
551
+ hasattr (ml , 'init' )])
552
+ def test_singular_covariance_init (estimator , build_dataset ):
553
+ """Tests that when using the 'covariance' init, it works even if the
554
+ covariance matrix is singular (see
555
+ https://github.com/metric-learn/metric-learn/issues/202)
556
+ """
557
+ input_data , labels , _ , X = build_dataset ()
558
+ model = clone (estimator )
559
+ set_random_state (model )
560
+ # We create a feature that is a linear combination of the first two
561
+ # features:
562
+ coefs = np .random .RandomState (42 ).randn (2 , 1 )
563
+ input_data = np .concatenate ([input_data , input_data [:, ..., :2 ]
564
+ .dot (coefs )],
565
+ axis = - 1 )
566
+
567
+ # Fitting the model should return no error
568
+ model .set_params (init = 'covariance' )
569
+ model .fit (input_data , labels )
0 commit comments