MCPcopy
hub / github.com/scikit-learn/scikit-learn / _assert_all_finite

Function _assert_all_finite

sklearn/utils/validation.py:107–144  ·  view source on GitHub ↗

Like assert_all_finite, but only for ndarray.

(
    X, allow_nan=False, msg_dtype=None, estimator_name=None, input_name=""
)

Source from the content-addressed store, hash-verified

105
106
107def _assert_all_finite(
108 X, allow_nan=False, msg_dtype=None, estimator_name=None, input_name=""
109):
110 """Like assert_all_finite, but only for ndarray."""
111
112 xp, is_array_api = get_namespace(X)
113
114 if _get_config()["assume_finite"]:
115 return
116
117 X = xp.asarray(X)
118
119 # for object dtype data, we only check for NaNs (GH-13254)
120 if not is_array_api and X.dtype == np.dtype("object") and not allow_nan:
121 if _object_dtype_isnan(X).any():
122 raise ValueError("Input contains NaN")
123
124 # We need only consider float arrays, hence can early return for all else.
125 if not xp.isdtype(X.dtype, ("real floating", "complex floating")):
126 return
127
128 # First try an O(n) time, O(1) space solution for the common case that
129 # everything is finite; fall back to O(n) space `np.isinf/isnan` or custom
130 # Cython implementation to prevent false positives and provide a detailed
131 # error message.
132 with np.errstate(over="ignore"):
133 first_pass_isfinite = xp.isfinite(xp.sum(X))
134 if first_pass_isfinite:
135 return
136
137 _assert_all_finite_element_wise(
138 X,
139 xp=xp,
140 allow_nan=allow_nan,
141 msg_dtype=msg_dtype,
142 estimator_name=estimator_name,
143 input_name=input_name,
144 )
145
146
147def _assert_all_finite_element_wise(

Callers 6

type_of_targetFunction · 0.90
fitMethod · 0.90
assert_all_finiteFunction · 0.85
_ensure_sparse_formatFunction · 0.85
check_arrayFunction · 0.85
_check_yFunction · 0.85

Calls 3

get_namespaceFunction · 0.90
_object_dtype_isnanFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…