Python | Numpy nanmedian() function
numpy.nanmedian() function can be used to calucate the median of array ignoring the NaN value. If array have NaN value and we can find out the median without effect of NaN value. Let’s see different type of examples about numpu.nanmedian() method.
Syntax: numpy.nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=)
Parametrs:
a: [arr_like] input array
axis: we can use axis=1 means row wise or axis=0 means column wise.
out: output array
overwrite_input: If True, then allow use of memory of input array a for calculations. The input array will be modified by the call to median.
keepdims: If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.
Returns: It return median in ndarray.
Example #1:
# Python code to demonstrate the # use of numpy.nanmedian import numpy as np # create 2d array with nan value. arr = np.array([[12, 10, 34], [45, 23, np.nan]]) print("Shape of array is", arr.shape) print("Median of array without using nanmedian function:", np.median(arr)) print("Using nanmedian function:", np.nanmedian(arr)) |
Output:
Shape of array is (2, 3) Median of array without using nanmedian function: nan Using nanmedian function: 23.0
Example #2:
# Python code to demonstrate the # use of numpy.nanmedian # with axis import numpy as np # create 2d array with nan value. arr = np.array([[12, 10, 34], [45, 23, np.nan]]) print("Shape of array is", arr.shape) print("Median of array with axis = 0:", np.median(arr, axis = 0)) print("Using nanmedian function:", np.nanmedian(arr, axis = 0)) |
Output:
Shape of array is (2, 3) Median of array with axis = 0: [ 28.5 16.5 nan] Using nanmedian function: [ 28.5 16.5 34. ]
Example #3:
# Python code to demonstrate the # use of numpy.nanmedian # with axis = 1 import numpy as np # create 2d matrix with nan value arr = np.array([[12, 10, 34], [45, 23, np.nan], [7, 8, np.nan]]) print("Shape of array is", arr.shape) print("Median of array with axis = 0:", np.median(arr, axis = 1)) print("Using nanmedian function:", np.nanmedian(arr, axis = 1)) |
Output:
Shape of array is (3, 3) Median of array with axis = 0: [ 12. nan nan] Using nanmedian function: [ 12. 34. 7.5]
Recommended Posts:
- sciPy stats.nanmedian() function | Python
- Python | numpy.cov() function
- Numpy recarray.var() function | Python
- Numpy recarray.mean() function | Python
- Python | numpy.issubdtype() function
- Numpy MaskedArray.dot() function | Python
- Numpy MaskedArray.all() function | Python
- Python | numpy.nanmean() function
- Python | numpy.issubsctype() function
- Numpy MaskedArray.mean() function | Python
- Numpy recarray.dot() function | Python
- Numpy MaskedArray.std() function | Python
- Numpy MaskedArray.var() function | Python
- Numpy MaskedArray.sum() function | Python
- Python | numpy.issctype() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



