scipy.stats.nanmedian(array, axis=0)
函数通过忽略沿数组指定轴的数组元素的Nan(不是数字)值来计算中位数。
参数:
array:具有元素(包括Nan值)的输入数组或对象,以计算中位数。
axis:计算中值所沿的轴。默认情况下轴= 0
返回:基于设置参数的数组元素的中位数(忽略Nan值)。
代码1:
# median
import scipy
import numpy as np
arr1 = [1, 3, np.nan, 27, 2, 5]
print("median using nanmedian:", scipy.nanmedian(arr1))
print("median without handling nan value:", scipy.median(arr1))
输出:
median using nanmedian:3.0 median without handling nan value:nan
代码2:多维数据
# median
from scipy import median
from scipy import nanmedian
import numpy as np
arr1 = [[1, 3, 27],
[3, np.nan, 6],
[np.nan, 6, 3],
[3, 6, np.nan]]
print("median is:", median(arr1))
print("median handling nan:", nanmedian(arr1))
# using axis = 0
print("\nmedian is with default axis = 0:\n",
median(arr1, axis = 0))
print("\nmedian handling nan with default axis = 0:\n",
nanmedian(arr1, axis = 0))
# using axis = 1
print("\nmedian is with default axis = 1:\n",
median(arr1, axis = 1))
print("\nmedian handling nan with default axis = 1:\n",
nanmedian(arr1, axis = 1))
输出:
median is:nan median handling nan:3.0 median is with default axis = 0: [ nan nan nan] median handling nan with default axis = 0: [ 3. 6. 6.] median is with default axis = 1: [ 3. nan nan nan] median handling nan with default axis = 1: [ 3. 4.5 4.5 4.5]
相关用法
- Python Scipy stats.mean()用法及代码示例
- Python Scipy stats.sem()用法及代码示例
- Python Scipy stats.tmin()用法及代码示例
- Python Scipy stats.tsem()用法及代码示例
- Python Scipy stats.binned_statistic_2d()用法及代码示例
- Python Scipy stats.describe()用法及代码示例
- Python Scipy stats.tmax()用法及代码示例
- Python Scipy stats.tstd()用法及代码示例
- Python Scipy stats.binned_statistic_dd()用法及代码示例
- Python Scipy stats.binned_statistic()用法及代码示例
- Python Scipy stats.bayes_mvs()用法及代码示例
- Python Scipy stats.kurtosistest()用法及代码示例
注:本文由纯净天空筛选整理自vishal3096大神的英文原创作品 sciPy stats.nanmedian() function | Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。