Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71953c4

Browse files
Fabian Pedregosalarsmans
Fabian Pedregosa
authored andcommitted
Safer assert_all_finite.
Check for Inf/NaN when X.sum() is not finite. This avoids the risk of false positives because of sum overflow while remaining performant for the usual case where all values are finite. This idea is taken from: tecki/numpy@57167f7
1 parent 5526951 commit 71953c4

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

sklearn/utils/__init__.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
import scipy.sparse as sp
33
import warnings
44

5-
_FLOAT_CODES = np.typecodes['AllFloat']
6-
7-
85
def assert_all_finite(X):
96
"""Throw a ValueError if X contains NaN or infinity.
107
Input MUST be an np.ndarray instance or a scipy.sparse matrix."""
118

12-
# O(n) time, O(1) solution. XXX: will fail if the sum over X is
13-
# *extremely* large. A proper solution would be a C-level loop to check
14-
# each element.
15-
if X.dtype.char in _FLOAT_CODES and not np.isfinite(X.sum()):
16-
raise ValueError("array contains NaN or infinity")
9+
# First try an O(n) time, O(1) space solution for the common case that
10+
# there everything is finite; fall back to O(n) space np.isfinite to
11+
# prevent false positives from overflow in sum method.
12+
if X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum()) \
13+
and not np.isfinite(X).all():
14+
raise ValueError("array contains NaN or infinity")
1715

1816

1917
def safe_asanyarray(X, dtype=None, order=None):

0 commit comments

Comments
 (0)