当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Scipy stats.nanmedian()用法及代码示例


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]


相关用法


注:本文由纯净天空筛选整理自vishal3096大神的英文原创作品 sciPy stats.nanmedian() function | Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。