187 lines
6.2 KiB
Python
187 lines
6.2 KiB
Python
|
"""
|
||
|
The :mod:`sklearn.exceptions` module includes all custom warnings and error
|
||
|
classes used across scikit-learn.
|
||
|
"""
|
||
|
|
||
|
__all__ = ['NotFittedError',
|
||
|
'ChangedBehaviorWarning',
|
||
|
'ConvergenceWarning',
|
||
|
'DataConversionWarning',
|
||
|
'DataDimensionalityWarning',
|
||
|
'EfficiencyWarning',
|
||
|
'FitFailedWarning',
|
||
|
'NonBLASDotWarning',
|
||
|
'SkipTestWarning',
|
||
|
'UndefinedMetricWarning',
|
||
|
'PositiveSpectrumWarning']
|
||
|
|
||
|
|
||
|
class NotFittedError(ValueError, AttributeError):
|
||
|
"""Exception class to raise if estimator is used before fitting.
|
||
|
|
||
|
This class inherits from both ValueError and AttributeError to help with
|
||
|
exception handling and backward compatibility.
|
||
|
|
||
|
Examples
|
||
|
--------
|
||
|
>>> from sklearn.svm import LinearSVC
|
||
|
>>> from sklearn.exceptions import NotFittedError
|
||
|
>>> try:
|
||
|
... LinearSVC().predict([[1, 2], [2, 3], [3, 4]])
|
||
|
... except NotFittedError as e:
|
||
|
... print(repr(e))
|
||
|
NotFittedError("This LinearSVC instance is not fitted yet. Call 'fit' with
|
||
|
appropriate arguments before using this estimator."...)
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.utils.validation.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class ChangedBehaviorWarning(UserWarning):
|
||
|
"""Warning class used to notify the user of any change in the behavior.
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.base.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class ConvergenceWarning(UserWarning):
|
||
|
"""Custom warning to capture convergence problems
|
||
|
|
||
|
Examples
|
||
|
--------
|
||
|
|
||
|
>>> import numpy as np
|
||
|
>>> import warnings
|
||
|
>>> from sklearn.cluster import KMeans
|
||
|
>>> from sklearn.exceptions import ConvergenceWarning
|
||
|
>>> warnings.simplefilter("always", ConvergenceWarning)
|
||
|
>>> X = np.asarray([[0, 0],
|
||
|
... [0, 1],
|
||
|
... [1, 0],
|
||
|
... [1, 0]]) # last point is duplicated
|
||
|
>>> with warnings.catch_warnings(record=True) as w:
|
||
|
... km = KMeans(n_clusters=4).fit(X)
|
||
|
... print(w[-1].message)
|
||
|
Number of distinct clusters (3) found smaller than n_clusters (4).
|
||
|
Possibly due to duplicate points in X.
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.utils.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class DataConversionWarning(UserWarning):
|
||
|
"""Warning used to notify implicit data conversions happening in the code.
|
||
|
|
||
|
This warning occurs when some input data needs to be converted or
|
||
|
interpreted in a way that may not match the user's expectations.
|
||
|
|
||
|
For example, this warning may occur when the user
|
||
|
- passes an integer array to a function which expects float input and
|
||
|
will convert the input
|
||
|
- requests a non-copying operation, but a copy is required to meet the
|
||
|
implementation's data-type expectations;
|
||
|
- passes an input whose shape can be interpreted ambiguously.
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.utils.validation.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class DataDimensionalityWarning(UserWarning):
|
||
|
"""Custom warning to notify potential issues with data dimensionality.
|
||
|
|
||
|
For example, in random projection, this warning is raised when the
|
||
|
number of components, which quantifies the dimensionality of the target
|
||
|
projection space, is higher than the number of features, which quantifies
|
||
|
the dimensionality of the original source space, to imply that the
|
||
|
dimensionality of the problem will not be reduced.
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.utils.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class EfficiencyWarning(UserWarning):
|
||
|
"""Warning used to notify the user of inefficient computation.
|
||
|
|
||
|
This warning notifies the user that the efficiency may not be optimal due
|
||
|
to some reason which may be included as a part of the warning message.
|
||
|
This may be subclassed into a more specific Warning class.
|
||
|
|
||
|
.. versionadded:: 0.18
|
||
|
"""
|
||
|
|
||
|
|
||
|
class FitFailedWarning(RuntimeWarning):
|
||
|
"""Warning class used if there is an error while fitting the estimator.
|
||
|
|
||
|
This Warning is used in meta estimators GridSearchCV and RandomizedSearchCV
|
||
|
and the cross-validation helper function cross_val_score to warn when there
|
||
|
is an error while fitting the estimator.
|
||
|
|
||
|
Examples
|
||
|
--------
|
||
|
>>> from sklearn.model_selection import GridSearchCV
|
||
|
>>> from sklearn.svm import LinearSVC
|
||
|
>>> from sklearn.exceptions import FitFailedWarning
|
||
|
>>> import warnings
|
||
|
>>> warnings.simplefilter('always', FitFailedWarning)
|
||
|
>>> gs = GridSearchCV(LinearSVC(), {'C': [-1, -2]}, error_score=0, cv=2)
|
||
|
>>> X, y = [[1, 2], [3, 4], [5, 6], [7, 8]], [0, 0, 1, 1]
|
||
|
>>> with warnings.catch_warnings(record=True) as w:
|
||
|
... try:
|
||
|
... gs.fit(X, y) # This will raise a ValueError since C is < 0
|
||
|
... except ValueError:
|
||
|
... pass
|
||
|
... print(repr(w[-1].message))
|
||
|
FitFailedWarning('Estimator fit failed. The score on this train-test
|
||
|
partition for these parameters will be set to 0.000000.
|
||
|
Details:...ValueError: Penalty term must be positive; got (C=-2)...
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.cross_validation.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class NonBLASDotWarning(EfficiencyWarning):
|
||
|
"""Warning used when the dot operation does not use BLAS.
|
||
|
|
||
|
This warning is used to notify the user that BLAS was not used for dot
|
||
|
operation and hence the efficiency may be affected.
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.utils.validation, extends EfficiencyWarning.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class SkipTestWarning(UserWarning):
|
||
|
"""Warning class used to notify the user of a test that was skipped.
|
||
|
|
||
|
For example, one of the estimator checks requires a pandas import.
|
||
|
If the pandas package cannot be imported, the test will be skipped rather
|
||
|
than register as a failure.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class UndefinedMetricWarning(UserWarning):
|
||
|
"""Warning used when the metric is invalid
|
||
|
|
||
|
.. versionchanged:: 0.18
|
||
|
Moved from sklearn.base.
|
||
|
"""
|
||
|
|
||
|
|
||
|
class PositiveSpectrumWarning(UserWarning):
|
||
|
"""Warning raised when the eigenvalues of a PSD matrix have issues
|
||
|
|
||
|
This warning is typically raised by ``_check_psd_eigenvalues`` when the
|
||
|
eigenvalues of a positive semidefinite (PSD) matrix such as a gram matrix
|
||
|
(kernel) present significant negative eigenvalues, or bad conditioning i.e.
|
||
|
very small non-zero eigenvalues compared to the largest eigenvalue.
|
||
|
|
||
|
.. versionadded:: 0.22
|
||
|
"""
|