You are viewing an older version (2.4.3). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.4.3 / numpy / nan_to_num

_ArrayFunctionDispatcher

numpy:nan_to_num

source: /numpy/lib/_type_check_impl.py :373

Signature

def   nan_to_num ( x copy = True nan = 0.0 posinf = None neginf = None )

Summary

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

Extended Summary

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.

Parameters

x : scalar or array_like

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 is True.

nan : int, float, or bool or array_like of int, float, or bool, optional

Values to be used to fill NaN values. If no values are passed then NaN values will be replaced with 0.0.

posinf : int, float, or bool or array_like of int, float, or bool, 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.

neginf : int, float, or bool or array_like of int, float, or bool, 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.

Returns

out : ndarray

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

Notes

NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.

Examples

import numpy as np
np.nan_to_num(np.inf)
np.nan_to_num(-np.inf)
np.nan_to_num(np.nan)
x = np.array([np.inf, -np.inf, np.nan, -128, 128])
np.nan_to_num(x)
np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333)
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)
y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)])
np.nan_to_num(y)
np.nan_to_num(y, nan=111111, posinf=222222)
nan = np.array([11, 12, 13])
posinf = np.array([21, 22, 23])
neginf = np.array([31, 32, 33])
np.nan_to_num(y, nan=nan, posinf=posinf, neginf=neginf)

See also

isfinite

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

isinf

Shows which elements are positive or negative infinity.

isnan

Shows which elements are Not a Number (NaN).

isneginf

Shows which elements are negative infinity.

isposinf

Shows which elements are positive infinity.

Aliases

  • numpy.nan_to_num

Referenced by

This package