numpy.nanmean()函数可用于计算忽略NaN值的数组平均值。如果数组具有NaN值,我们可以找出不受NaN值影响的均值。
用法: numpy.nanmean(a, axis=None, dtype=None, out=None, keepdims=))
参数:
a:[arr_like]输入数组
axis:我们可以使用axis = 1表示行方向或axis = 0表示列方向。
out:输出数组
dtype:数组的数据类型
overwrite_input:如果为True,则允许使用输入数组a的内存进行计算。输入数组将通过调用中位数进行修改。
keepdims:如果将其设置为True,则缩小的轴将保留为尺寸1的尺寸。使用此选项,结果将相对于原始a正确广播。
返回:返回数组元素的平均值
范例1:
# Python code to demonstrate the
# use of numpy.nanmean
import numpy as np
# create 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))
输出:
Shape of array is (2, 3) Mean of array without using nanmean function:nan Using nanmean function:26.4
范例2:
# Python code to demonstrate the
# use of numpy.nanmean
# with axis = 0
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]])
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))
输出:
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 ]
范例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([[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))
输出:
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. ]
相关用法
- Python sum()用法及代码示例
- Python id()用法及代码示例
- Python now()用法及代码示例
- Python hex()用法及代码示例
- Python cmp()用法及代码示例
- Python int()用法及代码示例
- Python oct()用法及代码示例
- Python tell()用法及代码示例
- Python map()用法及代码示例
- Python dir()用法及代码示例
- Python ord()用法及代码示例
注:本文由纯净天空筛选整理自shrikanth13大神的英文原创作品 Python | numpy.nanmean() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。