1"""
2The :mod:`sklearn.exceptions` module includes all custom warnings and error
3classes used across scikit-learn.
4"""
5
6__all__ = [
7 "NotFittedError",
8 "ConvergenceWarning",
9 "DataConversionWarning",
10 "DataDimensionalityWarning",
11 "EfficiencyWarning",
12 "FitFailedWarning",
13 "SkipTestWarning",
14 "UndefinedMetricWarning",
15 "PositiveSpectrumWarning",
16 "UnsetMetadataPassedError",
17]
18
19
20class UnsetMetadataPassedError(ValueError):
21 """Exception class to raise if a metadata is passed which is not explicitly \
22 requested.
23
24 .. versionadded:: 1.3
25
26 Parameters
27 ----------
28 message : str
29 The message
30
31 unrequested_params : dict
32 A dictionary of parameters and their values which are provided but not
33 requested.
34
35 routed_params : dict
36 A dictionary of routed parameters.
37 """
38
39 def __init__(self, *, message, unrequested_params, routed_params):
40 super().__init__(message)
41 self.unrequested_params = unrequested_params
42 self.routed_params = routed_params
43
44
45class NotFittedError(ValueError, AttributeError):
46 """Exception class to raise if estimator is used before fitting.
47
48 This class inherits from both ValueError and AttributeError to help with
49 exception handling and backward compatibility.
50
51 Examples
52 --------
53 >>> from sklearn.svm import LinearSVC
54 >>> from sklearn.exceptions import NotFittedError
55 >>> try:
56 ... LinearSVC().predict([[1, 2], [2, 3], [3, 4]])
57 ... except NotFittedError as e:
58 ... print(repr(e))
59 NotFittedError("This LinearSVC instance is not fitted yet. Call 'fit' with
60 appropriate arguments before using this estimator."...)
61
62 .. versionchanged:: 0.18
63 Moved from sklearn.utils.validation.
64 """
65
66
67class ConvergenceWarning(UserWarning):
68 """Custom warning to capture convergence problems
69
70 .. versionchanged:: 0.18
71 Moved from sklearn.utils.
72 """
73
74
75class DataConversionWarning(UserWarning):
76 """Warning used to notify implicit data conversions happening in the code.
77
78 This warning occurs when some input data needs to be converted or
79 interpreted in a way that may not match the user's expectations.
80
81 For example, this warning may occur when the user
82 - passes an integer array to a function which expects float input and
83 will convert the input
84 - requests a non-copying operation, but a copy is required to meet the
85 implementation's data-type expectations;
86 - passes an input whose shape can be interpreted ambiguously.
87
88 .. versionchanged:: 0.18
89 Moved from sklearn.utils.validation.
90 """
91
92
93class DataDimensionalityWarning(UserWarning):
94 """Custom warning to notify potential issues with data dimensionality.
95
96 For example, in random projection, this warning is raised when the
97 number of components, which quantifies the dimensionality of the target
98 projection space, is higher than the number of features, which quantifies
99 the dimensionality of the original source space, to imply that the
100 dimensionality of the problem will not be reduced.
101
102 .. versionchanged:: 0.18
103 Moved from sklearn.utils.
104 """
105
106
107class EfficiencyWarning(UserWarning):
108 """Warning used to notify the user of inefficient computation.
109
110 This warning notifies the user that the efficiency may not be optimal due
111 to some reason which may be included as a part of the warning message.
112 This may be subclassed into a more specific Warning class.
113
114 .. versionadded:: 0.18
115 """
116
117
118class FitFailedWarning(RuntimeWarning):
119 """Warning class used if there is an error while fitting the estimator.
120
121 This Warning is used in meta estimators GridSearchCV and RandomizedSearchCV
122 and the cross-validation helper function cross_val_score to warn when there
123 is an error while fitting the estimator.
124
125 .. versionchanged:: 0.18
126 Moved from sklearn.cross_validation.
127 """
128
129
130class SkipTestWarning(UserWarning):
131 """Warning class used to notify the user of a test that was skipped.
132
133 For example, one of the estimator checks requires a pandas import.
134 If the pandas package cannot be imported, the test will be skipped rather
135 than register as a failure.
136 """
137
138
139class UndefinedMetricWarning(UserWarning):
140 """Warning used when the metric is invalid
141
142 .. versionchanged:: 0.18
143 Moved from sklearn.base.
144 """
145
146
147class PositiveSpectrumWarning(UserWarning):
148 """Warning raised when the eigenvalues of a PSD matrix have issues
149
150 This warning is typically raised by ``_check_psd_eigenvalues`` when the
151 eigenvalues of a positive semidefinite (PSD) matrix such as a gram matrix
152 (kernel) present significant negative eigenvalues, or bad conditioning i.e.
153 very small non-zero eigenvalues compared to the largest eigenvalue.
154
155 .. versionadded:: 0.22
156 """
157
158
159class InconsistentVersionWarning(UserWarning):
160 """Warning raised when an estimator is unpickled with a inconsistent version.
161
162 Parameters
163 ----------
164 estimator_name : str
165 Estimator name.
166
167 current_sklearn_version : str
168 Current scikit-learn version.
169
170 original_sklearn_version : str
171 Original scikit-learn version.
172 """
173
174 def __init__(
175 self, *, estimator_name, current_sklearn_version, original_sklearn_version
176 ):
177 self.estimator_name = estimator_name
178 self.current_sklearn_version = current_sklearn_version
179 self.original_sklearn_version = original_sklearn_version
180
181 def __str__(self):
182 return (
183 f"Trying to unpickle estimator {self.estimator_name} from version"
184 f" {self.original_sklearn_version} when "
185 f"using version {self.current_sklearn_version}. This might lead to breaking"
186 " code or "
187 "invalid results. Use at your own risk. "
188 "For more info please refer to:\n"
189 "https://scikit-learn.org/stable/model_persistence.html"
190 "#security-maintainability-limitations"
191 )