Skip to content

Commit d622fae

Browse files
author
William de Vazelhes
committed
Add test for singular covariance matrix
1 parent 95a86a9 commit d622fae

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/test_mahalanobis_mixin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ def test_init_mahalanobis(estimator, build_dataset):
487487
"""
488488
input_data, labels, _, X = build_dataset()
489489
model = clone(estimator)
490+
set_random_state(model)
490491
rng = np.random.RandomState(42)
491492

492493
# Start learning from scratch
@@ -535,3 +536,34 @@ def test_init_mahalanobis(estimator, build_dataset):
535536
with pytest.raises(ValueError) as raised_error:
536537
model.fit(input_data, labels)
537538
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

Comments
 (0)