![]() |
VOOZH | about |
numpy.nan_to_num() function replaces NaN (Not a Number) with a specified numerical value (default is 0), and optionally converts positive or negative infinity to finite numbers.
Example:
[ 1.00000000e+000 0.00000000e+000 1.79769313e+308 -1.79769313e+308]
Explanation: By default, NaN is replaced with 0. Infinity values remain unchanged unless specified.
numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)
Parameters:
Returns: This method returns an array with the same shape as x, where NaN, +inf and -inf are replaced with finite numbers.
Example 1: Replace only NaNs with zero
[0. 2. 3.]
Explanation: NaN is replaced with 0, other values are unchanged.
Example 2: Replace NaN and infinite values
[ 0. 1000. -1000. 5.]
Explanation: NaN → 0, +inf → 1000, -inf → -1000 and regular values like 5 remain unchanged.
Example 3: In-place modification with copy=False
[-1.000e+00 9.999e+03]
Explanation: The original array 'a' is modified directly without making a copy.
Related articles:Numpy