dpnp.nan_to_num

dpnp.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)[source]

Replace NaN with zero and infinity with large finite numbers (default behavior) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.

If x is inexact, NaN is replaced by zero or by the user defined value in nan keyword, infinity is replaced by the largest finite floating point values representable by x.dtype or by the user defined value in posinf keyword and -infinity is replaced by the most negative finite floating point values representable by x.dtype or by the user defined value in neginf keyword.

For complex dtypes, the above is applied to each of the real and imaginary components of x separately.

If x is not inexact, then no replacements are made.

For full documentation refer to numpy.nan_to_num.

Parameters:
  • x ({dpnp.ndarray, usm_ndarray}) -- Input data.

  • copy (bool, optional) --

    Whether to create a copy of x (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy.

    Default: True.

  • nan ({scalar, array_like}, optional) --

    Values to be used to fill NaN values. If no values are passed then NaN values will be replaced with 0.0. Expected to have a real-valued data type for the values.

    Default: 0.0.

  • posinf ({None, scalar, array_like}, optional) --

    Values to be used to fill positive infinity values. If no values are passed then positive infinity values will be replaced with a very large number. Expected to have a real-valued data type for the values.

    Default: None.

  • neginf ({None, scalar, array_like}, optional) --

    Values to be used to fill negative infinity values. If no values are passed then negative infinity values will be replaced with a very small (or negative) number. Expected to have a real-valued data type for the values.

    Default: None.

Returns:

out -- x, with the non-finite values replaced. If copy is False, this may be x itself.

Return type:

dpnp.ndarray

See also

dpnp.isinf

Shows which elements are positive or negative infinity.

dpnp.isneginf

Shows which elements are negative infinity.

dpnp.isposinf

Shows which elements are positive infinity.

dpnp.isnan

Shows which elements are Not a Number (NaN).

dpnp.isfinite

Shows which elements are finite (not NaN, not infinity)

Examples

>>> import dpnp as np
>>> np.nan_to_num(np.array(np.inf))
array(1.79769313e+308)
>>> np.nan_to_num(np.array(-np.inf))
array(-1.79769313e+308)
>>> np.nan_to_num(np.array(np.nan))
array(0.)
>>> x = np.array([np.inf, -np.inf, np.nan, -128, 128])
>>> np.nan_to_num(x)
array([ 1.79769313e+308, -1.79769313e+308,  0.00000000e+000,
       -1.28000000e+002,  1.28000000e+002])
>>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333)
array([ 3.3333333e+07,  3.3333333e+07, -9.9990000e+03, -1.2800000e+02,
        1.2800000e+02])
>>> nan = np.array([11, 12, -9999, 13, 14])
>>> posinf = np.array([33333333, 11, 12, 13, 14])
>>> neginf = np.array([11, 33333333, 12, 13, 14])
>>> np.nan_to_num(x, nan=nan, posinf=posinf, neginf=neginf)
array([ 3.3333333e+07,  3.3333333e+07, -9.9990000e+03, -1.2800000e+02,
        1.2800000e+02])
>>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)])
>>> np.nan_to_num(y)
array([1.79769313e+308 +0.00000000e+000j, # may vary
       0.00000000e+000 +0.00000000e+000j,
       0.00000000e+000 +1.79769313e+308j])
>>> np.nan_to_num(y, nan=111111, posinf=222222)
array([222222.+111111.j, 111111.     +0.j, 111111.+222222.j])