numpy.nan_to_num() in Python Last Updated : 28 Nov, 2018 Comments Improve Suggest changes Like Article Like Report numpy.nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array. It returns (positive) infinity with a very large number and negative infinity with a very small (or negative) number. Syntax : numpy.nan_to_num(arr, copy=True) Parameters : arr : [array_like] Input data. copy : [bool, optional] Whether to create a copy of arr (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. Return : [ndarray] New Array with the same shape as arr and dtype of the element in arr with the greatest precision. If arr is inexact, then NaN is replaced by zero, and infinity (-infinity) is replaced by the largest (smallest or most negative) floating point value that fits in the output dtype. If arr is not inexact, then a copy of arr is returned. Code #1 : Working Python # Python program explaining # numpy.nan_to_num() function import numpy as geek in_num = geek.nan print ("Input number : ", in_num) out_num = geek.nan_to_num(in_num) print ("output number : ", out_num) Output : Input number : nan output number : 0.0 Code #2 : Python # Python program explaining # numpy.nan_to_num function import numpy as geek in_arr = geek.array([[2, geek.inf, 2], [2, 2, geek.nan]]) print ("Input array : ", in_arr) out_arr = geek.nan_to_num(in_arr) print ("output array: ", out_arr) Output : Input array : [[ 2. inf 2.] [ 2. 2. nan]] output array: [[ 2.00000000e+000 1.79769313e+308 2.00000000e+000] [ 2.00000000e+000 2.00000000e+000 0.00000000e+000]] Code #3 : Python # Python program explaining # numpy.nan_to_num function import numpy as geek in_arr = geek.array([[2, 2, 2], [2, 2, 6]]) print ("Input array : ", in_arr) out_arr = geek.nan_to_num(in_arr) print ("Output array: ", out_arr) Output : Input array : Input array : [[2 2 2] [2 2 6]] Output array: [[2 2 2] [2 2 6]] Comment More infoAdvertise with us Next Article numpy.nan_to_num() in Python jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-Mathematical Function Practice Tags : python Similar Reads numpy.nansum() in Python numpy.nansum()function is used when we want to compute the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. Syntax : numpy.nansum(arr, axis=None, dtype=None, out=None, keepdims='no value') Parameters : arr : [array_like] Array containing numbers whose sum is desired. If 3 min read numpy.nanmin() in Python numpy.nanmin()function is used when to returns minimum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmin(arr, axis=None, out=None) Parameters : arr :Input array. axis :Axis along which we want the min value. Otherwise, it will consider 2 min read numpy.nancumsum() in Python numpy.nancumsum() function is used when we want to compute the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN 3 min read numpy.isnan() in Python The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax :Â numpy.isnan(array [, out]) Parameters :Â array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array placed wit 2 min read numpy.nancumprod() in Python numpy.nancumprod() function is used when we want to compute the cumulative product of array elements over a given axis treating Not a Numbers (NaNs) as one. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are a 3 min read numpy.nanvar() in Python numpy.nanvar(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any), while ignoring NaN values. Example : x = 1 1 1 1 1 Standard Deviation = 0 . Variance = 0 y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of dist 3 min read numpy.nanprod() in Python numpy.nanprod() function is used when we want to compute the product of array elements over a given axis treating NaNs as ones. One is returned for slices that are all-NaN or empty. Syntax : numpy.nanprod(arr, axis=None, dtype=None, out=None, keepdims='class numpy._globals._NoValue'). Parameters : a 2 min read np.nanmax() in Python numpy.nanmax()function is used to returns maximum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmax(arr, axis=None, out=None, keepdims = no value) Parameters : arr : Input array. axis : Axis along which we want the max value. Otherwise 2 min read numpy.sqrt() in Python numpy.sqrt() in Python is a function from the NumPy library used to compute the square root of each element in an array or a single number. It returns a new array of the same shape with the square roots of the input values. The function handles both positive and negative numbers, returning NaN for n 2 min read numpy.nonzero() in Python numpy.nonzero()function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(ar 2 min read Like