nan_to_num#

ivy.nan_to_num(x, /, *, copy=True, nan=0.0, posinf=None, neginf=None, out=None)[source]#

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.

Parameters:
  • x (Union[Array, NativeArray]) – Array input.

  • copy (bool, default: True) – 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 (Union[float, int], default: 0.0) – Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0.

  • posinf (Optional[Union[int, float]], default: None) – Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.

  • neginf (Optional[Union[int, float]], default: None) – Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

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

Examples

>>> x = ivy.array([1, 2, 3, nan])
>>> ivy.nan_to_num(x)
ivy.array([1.,    1.,   3.,   0.0])
>>> x = ivy.array([1, 2, 3, inf])
>>> ivy.nan_to_num(x, posinf=5e+100)
ivy.array([1.,   2.,   3.,   5e+100])
Array.nan_to_num(self, /, *, copy=True, nan=0.0, posinf=None, neginf=None, out=None)[source]#

ivy.Array instance method variant of ivy.nan_to_num. This method simply wraps the function, and so the docstring for ivy.nan_to_num also applies to this method with minimal changes.

Parameters:
  • self (Array) – Array input.

  • copy (bool, default: True) – 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 (Union[float, int], default: 0.0) – Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0.

  • posinf (Optional[Union[int, float]], default: None) – Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.

  • neginf (Optional[Union[int, float]], default: None) – Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

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

Examples

>>> x = ivy.array([1, 2, 3, nan])
>>> x.nan_to_num()
ivy.array([1.,    1.,   3.,   0.0])
>>> x = ivy.array([1, 2, 3, inf])
>>> x.nan_to_num(posinf=5e+100)
ivy.array([1.,   2.,   3.,   5e+100])
Container.nan_to_num(self, /, *, copy=True, nan=0.0, posinf=None, neginf=None, out=None)[source]#

ivy.Container instance method variant of ivy.nan_to_num. This method simply wraps the function, and so the docstring for ivy.nan_to_num also applies to this method with minimal changes.

Parameters:
  • self (Container) – Input container with array items.

  • copy (Union[bool, Container], default: True) – 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 (Union[float, int, Container], default: 0.0) – Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0.

  • posinf (Optional[Union[int, float, Container]], default: None) – Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.

  • neginf (Optional[Union[int, float, Container]], default: None) – Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.

  • out (Optional[Container], default: None) – optional output container, for writing the result to.

Return type:

Container

Returns:

ret – Container including arrays with replaced non-finite elements.

Examples

>>> a = ivy.array([1., 2, 3, ivy.nan], dtype="float64")
>>> b = ivy.array([1., 2, 3, ivy.inf], dtype="float64")
>>> x = ivy.Container(a=a, b=b)
>>> ret = x.nan_to_num(posinf=5e+100)
>>> print(ret)
{
    a: ivy.array([1., 2., 3., 0.]),
    b: ivy.array([1.e+000, 2.e+000, 3.e+000, 5.e+100])
}