Throw a ValueError if X contains NaN or infinity. Parameters ---------- X : {ndarray, sparse matrix} The input data. allow_nan : bool, default=False If True, do not throw error when `X` contains NaN. estimator_name : str, default=None The estimator name
(
X,
*,
allow_nan=False,
estimator_name=None,
input_name="",
)
| 187 | |
| 188 | |
| 189 | def assert_all_finite( |
| 190 | X, |
| 191 | *, |
| 192 | allow_nan=False, |
| 193 | estimator_name=None, |
| 194 | input_name="", |
| 195 | ): |
| 196 | """Throw a ValueError if X contains NaN or infinity. |
| 197 | |
| 198 | Parameters |
| 199 | ---------- |
| 200 | X : {ndarray, sparse matrix} |
| 201 | The input data. |
| 202 | |
| 203 | allow_nan : bool, default=False |
| 204 | If True, do not throw error when `X` contains NaN. |
| 205 | |
| 206 | estimator_name : str, default=None |
| 207 | The estimator name, used to construct the error message. |
| 208 | |
| 209 | input_name : str, default="" |
| 210 | The data name used to construct the error message. In particular |
| 211 | if `input_name` is "X" and the data has NaN values and |
| 212 | allow_nan is False, the error message will link to the imputer |
| 213 | documentation. |
| 214 | |
| 215 | Examples |
| 216 | -------- |
| 217 | >>> from sklearn.utils import assert_all_finite |
| 218 | >>> import numpy as np |
| 219 | >>> array = np.array([1, np.inf, np.nan, 4]) |
| 220 | >>> try: |
| 221 | ... assert_all_finite(array) |
| 222 | ... print("Test passed: Array contains only finite values.") |
| 223 | ... except ValueError: |
| 224 | ... print("Test failed: Array contains non-finite values.") |
| 225 | Test failed: Array contains non-finite values. |
| 226 | """ |
| 227 | _assert_all_finite( |
| 228 | X.data if sp.issparse(X) else X, |
| 229 | allow_nan=allow_nan, |
| 230 | estimator_name=estimator_name, |
| 231 | input_name=input_name, |
| 232 | ) |
| 233 | |
| 234 | |
| 235 | def as_float_array(X, *, copy=True, ensure_all_finite=True): |
searching dependent graphs…