Skip to content
Snippets Groups Projects
Commit 71953c49 authored by Fabian Pedregosa's avatar Fabian Pedregosa Committed by Lars Buitinck
Browse files

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:

   https://github.com/tecki/numpy/commit/57167f7c02b02bfb49204233eaee0f289ad37c92
parent 55269519
No related branches found
No related tags found
No related merge requests found
......@@ -2,17 +2,15 @@ import numpy as np
import scipy.sparse as sp
import warnings
_FLOAT_CODES = np.typecodes['AllFloat']
def assert_all_finite(X):
"""Throw a ValueError if X contains NaN or infinity.
Input MUST be an np.ndarray instance or a scipy.sparse matrix."""
# O(n) time, O(1) solution. XXX: will fail if the sum over X is
# *extremely* large. A proper solution would be a C-level loop to check
# each element.
if X.dtype.char in _FLOAT_CODES and not np.isfinite(X.sum()):
# First try an O(n) time, O(1) space solution for the common case that
# there everything is finite; fall back to O(n) space np.isfinite to
# prevent false positives from overflow in sum method.
if X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.sum()) \
and not np.isfinite(X).all():
raise ValueError("array contains NaN or infinity")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment