forked from scikit-learn/scikit-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.py
249 lines (187 loc) · 7.52 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""Custom warnings and errors used across scikit-learn."""
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
__all__ = [
"NotFittedError",
"ConvergenceWarning",
"DataConversionWarning",
"DataDimensionalityWarning",
"EfficiencyWarning",
"FitFailedWarning",
"SkipTestWarning",
"UndefinedMetricWarning",
"PositiveSpectrumWarning",
"UnsetMetadataPassedError",
"EstimatorCheckFailedWarning",
]
class UnsetMetadataPassedError(ValueError):
"""Exception class to raise if a metadata is passed which is not explicitly \
requested (metadata=True) or not requested (metadata=False).
.. versionadded:: 1.3
Parameters
----------
message : str
The message
unrequested_params : dict
A dictionary of parameters and their values which are provided but not
requested.
routed_params : dict
A dictionary of routed parameters.
"""
def __init__(self, *, message, unrequested_params, routed_params):
super().__init__(message)
self.unrequested_params = unrequested_params
self.routed_params = routed_params
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 ConvergenceWarning(UserWarning):
"""Custom warning to capture convergence problems
.. 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.
.. versionchanged:: 0.18
Moved from sklearn.cross_validation.
"""
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
"""
class InconsistentVersionWarning(UserWarning):
"""Warning raised when an estimator is unpickled with a inconsistent version.
Parameters
----------
estimator_name : str
Estimator name.
current_sklearn_version : str
Current scikit-learn version.
original_sklearn_version : str
Original scikit-learn version.
"""
def __init__(
self, *, estimator_name, current_sklearn_version, original_sklearn_version
):
self.estimator_name = estimator_name
self.current_sklearn_version = current_sklearn_version
self.original_sklearn_version = original_sklearn_version
def __str__(self):
return (
f"Trying to unpickle estimator {self.estimator_name} from version"
f" {self.original_sklearn_version} when "
f"using version {self.current_sklearn_version}. This might lead to breaking"
" code or "
"invalid results. Use at your own risk. "
"For more info please refer to:\n"
"https://scikit-learn.org/stable/model_persistence.html"
"#security-maintainability-limitations"
)
class EstimatorCheckFailedWarning(UserWarning):
"""Warning raised when an estimator check from the common tests fails.
Parameters
----------
estimator : estimator object
Estimator instance for which the test failed.
check_name : str
Name of the check that failed.
exception : Exception
Exception raised by the failed check.
status : str
Status of the check.
expected_to_fail : bool
Whether the check was expected to fail.
expected_to_fail_reason : str
Reason for the expected failure.
"""
def __init__(
self,
*,
estimator,
check_name: str,
exception: Exception,
status: str,
expected_to_fail: bool,
expected_to_fail_reason: str,
):
self.estimator = estimator
self.check_name = check_name
self.exception = exception
self.status = status
self.expected_to_fail = expected_to_fail
self.expected_to_fail_reason = expected_to_fail_reason
def __repr__(self):
expected_to_fail_str = (
f"Expected to fail: {self.expected_to_fail_reason}"
if self.expected_to_fail
else "Not expected to fail"
)
return (
f"Test {self.check_name} failed for estimator {self.estimator!r}.\n"
f"Expected to fail reason: {expected_to_fail_str}\n"
f"Exception: {self.exception}"
)
def __str__(self):
return self.__repr__()