 # Python | numpy.nanmean () function

The numpy.nanmean () function can be used to compute the average of an array ignoring the NaN value. If the array is NaN and we can know the average without being influenced by the NaN value.

Syntax: numpy.nanmean (a, axis = None, dtype = None, out = None, 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
dtype: data types of 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: Returns the average of the array elements

Example # 1:

 ` # Python code for demonstration ` ` # using numpy.nanmean ` ` import ` ` numpy as np `   ` # create a 2d array with nan value. ` ` arr ` ` = ` ` np.array ([[` ` 20 , 15 , 37 ], [ 47 ,  13 , np.nan]]) ````   print ( "Shape of array is" , arr.shape)     print ( "Mean of array without using nanmean function:" ,  np.mean (arr))    print ( "Using nanmean function:" , np.nanmean (arr))  ```

Output :

` Shape of array is (2, 3) Mean of array without using nanmean function: nan Using nanmean function: 26.4 `

Example # 2:

` `

``` # Python code for demo # using numpy.nanmean # with axis = 0 import numpy as np    # create a 2d matrix with nan value arr = np.array ([[ 32 , 20 , 24 ],  [ 47 , 63 , np.nan],  [ 17 , 28 , np.nan], [ 10 , 8 , 9 ]])    print ( "Shape of array is" , arr.shape)    print ( "Mean of array with axis = 0:" ,    np.mean (arr, axis = 0 ))     print ( " Using nanmedian function: " ,    np.nanmean (arr, axis = 0 ))  ```

` ` Exit:

` Shape of array is (4, 3) Mean of array with axis = 0: [26.5 29.75 nan] Using nanmedian function: [26.5 29.75 16.5] `

Example # 3:

` `

``` < / table> Output : Shape of array is (4, 3) Mean of array with axis = 1: [25.33333333 nan nan 9.] Using nanmedian function: [25.33333333 55. 22.5 9.] (adsbygoogle = window.adsbygoogle || []).push({}); © 2021 Python.Engineering Best Python tutorials books for beginners and professionals Become an author and write for us Architect Development For dummies Machine Learning Analysis Loops Counters NumPy NLP Regular Expressions File Handling Arrays String Variables Knowledge Database X Submit new EBook # Python code for demonstration # using numpy.nanmedian # with axis = 1 import numpy as np     # create 2d matrix with nan value arr = np.array ([[ 32 , 20 , 24 ] ,  [ 47 , 63 , np.nan],  [ 17 , 28 , np.nan],   [ 10 , 8 , 9 ]]) < / code>   print ( "Shape of array is" , arr.shape)    print ( " Mean of array with axis = 1: " ,    np.mean (arr, axis = 1 ))     print ( "Using nanmedian function:" ,  np.nanmean (arr, axis = 1 ))  \$(document).ready(function () { \$(".modal_galery").owlCarousel({ items: 1, itemsCustom: false, itemsDesktop: [1300, 1], itemsDesktopSmall: [960, 1], itemsTablet: [768, 1], itemsTabletSmall: false, itemsMobile: [479, 1], singleItem: false, itemsScaleUp: false, pagination: false, navigation: true, rewindNav: true, autoPlay: true, stopOnHover: true, navigationText: [ "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/prevCopy.png'>", "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/nextCopy.png'>" ], }); \$(".tel_mask").mask("+9(999) 999-99-99"); }) ```