check_is_finite#

pybear.base.check_is_finite(X, allow_nan=True, allow_inf=True, cast_inf_to_nan=True, standardize_nan=True, copy_X=True)#

Look for any nan-like and/or infinity-like values in X.

If any of these are disallowed then raise a ValueError if any are present.

If cast_inf_to_nan is True, all infinity-like values will be cast to np.nan, otherwise they are left as is.

If standardize_nan is True, all nan-like values will be cast to np.nan, otherwise they are left as is.

X cannot be a Python builtin iterable, like list or set. X must have a copy method.

Parameters:
XXContainer of shape (n_samples, n_features) or (n_samples,)

The data to be searched for nan-like and infinity-like values.

allow_nanbool, default=True

If nan-like values are found and this parameter is set to False then raise a ValueError.

allow_infbool, default=True

If infinity-like values are found and this parameter is set to False then raise a ValueError.

cast_inf_to_nanbool, default=True

If True, all infinity-like values will be cast to np.nan.

standardize_nanbool, default=True

If True, all nan-like values will be cast to np.nan.

copy_Xbool

If True, make a copy of X if any infinity-likes are cast to np.nan or if any nan-likes are cast to np.nan. If False, operate directly on the passed X object. Only applicable if either cast_inf_to_nan or standardize_nan is True and there are infinity-like or nan-like values in the data.

Returns:
Xnpt.NDArray | pd.DataFrame | SparseTypes

The originally passed data with all checks performed and any replacements made.

Notes

Type Aliases

SparseTypes:

ss._csr.csr_matrix | ss._csc.csc_matrix | ss._coo.coo_matrix | ss._dia.dia_matrix | ss._bsr.bsr_matrix | ss._csr.csr_array | ss._csc.csc_array | ss._coo.coo_array | ss._dia.dia_array | ss._bsr.bsr_array

XContainer:

numpy.ndarray | pandas.DataFrame | SparseTypes

Examples

>>> from pybear.base import check_is_finite
>>> import numpy as np
>>> X = np.random.uniform(0, 1, (5, 3))
>>> kwargs = {'allow_nan': False, 'allow_inf': False,
...   'cast_inf_to_nan': False, 'standardize_nan': False}
>>> out = check_is_finite(X, **kwargs)
>>> type(out)
<class 'numpy.ndarray'>
>>> X[0, 0] = np.nan
>>> try:
...     check_is_finite(X, **kwargs)
... except Exception as e:
...     print(repr(e))
ValueError("'X' has nan-like values but are disallowed")