Skip to content

SCML: Raise ValueError if n_features larger than n_triplets #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions metric_learn/scml.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ def _generate_bases_dist_diff(self, triplets, X):
raise ValueError("n_basis should be an integer, instead it is of type %s"
% type(self.n_basis))

if n_features > n_triplets:
raise ValueError(
"Number of features (%s) is greater than the number of triplets(%s).\n"
"Consider using dimensionality reduction or using another basis "
"generation scheme." % (n_features, n_triplets))

Copy link
Contributor

@grudloff grudloff Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I would reformulate it to

"Consider using dimensionality reduction or using another basis generation scheme."

This way we consider possible future basis generation alternatives as well as just passing in a basis set directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, updated

basis = np.zeros((n_basis, n_features))

# get all positive and negative pairs with lowest index first
Expand Down
20 changes: 19 additions & 1 deletion test/test_triplets_classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
from sklearn.exceptions import NotFittedError
from sklearn.model_selection import train_test_split

from test.test_utils import triplets_learners, ids_triplets_learners
from metric_learn import SCML
from test.test_utils import (
triplets_learners,
ids_triplets_learners,
build_triplets
)
from metric_learn.sklearn_shims import set_random_state
from sklearn import clone
import numpy as np
Expand Down Expand Up @@ -107,3 +112,16 @@ def test_accuracy_toy_example(estimator, build_dataset):
# we force the transformation to be identity so that we control what it does
estimator.components_ = np.eye(X.shape[1])
assert estimator.score(triplets_test) == 0.25


def test_raise_big_number_of_features():
triplets, _, _, X = build_triplets(with_preprocessor=False)
triplets = triplets[:3, :, :]
estimator = SCML(n_basis=320)
set_random_state(estimator)
with pytest.raises(ValueError) as exc_info:
estimator.fit(triplets)
assert exc_info.value.args[0] == \
"Number of features (4) is greater than the number of triplets(3)." \
"\nConsider using dimensionality reduction or using another basis " \
"generation scheme."