Skip to content

More informative error when calling transform of unfitted metric learner #255

Closed
@bellet

Description

@bellet

We should throw an informative error/exception when one attempts to use an unfitted transformer to transform data (or any other method which requires the metric learner to be fitted). See below what happens in metric-learn versus sklearn. We can probably borrow some code from sklearn to handle this similarly.

Example for metric-learn:

>>> from metric_learn import NCA
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification()
>>> nca = NCA()
>>> nca.transform(X)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aurelien/Documents/research/github/metric-learn/metric_learn/base_metric.py", line 244, in transform
    preprocessor=self.preprocessor_,
AttributeError: 'NCA' object has no attribute 'preprocessor_'

Example for sklearn:

>>> from sklearn.decomposition import PCA
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification()
>>> pca = PCA()
>>> pca.transform(X)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aurelien/anaconda3/lib/python3.7/site-packages/sklearn/decomposition/base.py", line 125, in transform
    check_is_fitted(self, ['mean_', 'components_'], all_or_any=all)
  File "/home/aurelien/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py", line 914, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This PCA instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions