Python | numpy.nanmean() function (original) (raw)
Last Updated : 01 Jun, 2021
numpy.nanmean() function can be used to calculate the mean of array ignoring the NaN value. If array have NaN value and we can find out the mean without effect of NaN value.
Syntax: numpy.nanmean(a, axis=None, dtype=None, out=None, keepdims=))
Parameters:
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:
Python3
import
numpy as np
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:
Python3
import
numpy as np
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
))
Output:
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:
Python3
import
numpy as np
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 = 1:"
,
`` np.mean(arr, axis
=
1
))
print
(
"Using nanmedian function:"
,
`` np.nanmean(arr, axis
=
1
))
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. ]
Similar Reads
- Python | Numpy nanmedian() function numpy.nanmedian() function can be used to calculate 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 numpy.nanmedian() method. Syntax: numpy.nanmedian(a, axis=None, out=None, ove 2 min read
- numpy.nanstd() function - Python numpy.nanstd() function compute the standard deviation along the specified axis, while ignoring NaNs. Syntax : numpy.nanstd(arr, axis = None, dtype = None, out = None, ddof = 0, keepdims) Parameters : arr : [array_like] Calculate the standard deviation of the non-NaN values. axis : [{int, tuple of i 2 min read
- numpy.imag() function - Python numpy.imag() function return the imaginary part of the complex argument. Syntax : numpy.imag(arr) Parameters : arr : [array_like] Input array. Return : [ndarray or scalar] The imaginary component of the complex argument. If val is real, the type of val is used for the output. If val has complex elem 1 min read
- numpy.i0() function | Python numpy.i0() function is the modified Bessel function of the first kind, order 0. it's usually denoted by I0. Syntax : numpy.i0(x) Parameters : x : [array_like, dtype float or complex] Argument of the Bessel function. Return : [ndarray, shape = x.shape, dtype = x.dtype] The modified Bessel function ev 1 min read
- numpy.ma.make_mask() function | Python numpy.ma.make_mask() function is used to create a boolean mask from an array. This function can accept any sequence that is convertible to integers, or nomask. It does not require that contents must be 0s and 1s, values of 0 are interpreted as False, everything else as True. Return m as a boolean ma 2 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.pad() function in Python numpy.pad() function is used to pad the Numpy arrays. Sometimes there is a need to perform padding in Numpy arrays, then numPy.pad() function is used. The function returns the padded array of rank equal to the given array and the shape will increase according to pad_width. Syntax: numpy.pad(array, p 2 min read
- Numpy ndarray.dot() function | Python The numpy.ndarray.dot() function computes the dot product of two arrays. It is widely used in linear algebra, machine learning and deep learning for operations like matrix multiplication and vector projections. Example: [GFGTABS] Python import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 2 min read
- numpy.matrix.A() function - Python numpy.matrix.A() function return self as an ndarray object. Syntax : numpy.matrix.A() Parameters : None Return : [ndarray] Return self as an ndarray. Code #1 : # Python program explaining # numpy.matrix.A() function # importing numpy as geek import numpy as geek mat = geek.matrix(geek.arange(9).resh 1 min read
- numpy.ma.fix_invalid() function | Python numpy.ma.fix_invalid() function return input with invalid data masked and replaced by a fill value. Where invalid data means values of nan, inf, etc. Syntax : numpy.ma.fix_invalid(arr, mask = False, copy = True, fill_value = None) Parameter : arr : [array_like] Input array. mask : [sequence, optiona 2 min read