【问题标题】:Calculate mannwhitneyu over specified numpy axis在指定的 numpy 轴上计算 mannwhitneyu
【发布时间】:2014-12-01 14:06:28
【问题描述】:

Mann-Witney-U 检验是 t 检验的非参数等效方法,只能用于正态分布数据。在 Scipy 中,两个测试都可用:

t 检验:http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html

Mann-Witney-U 测试:http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.mannwhitneyu.html

虽然 ttest 可以通过例如通过指定轴来执行计算的 3d 数组,这对于 Mann-Witney-U-test 似乎是不可能的?

import numpy
from scipy import stats

A1= numpy.random.normal(1,1,50).reshape(25, 2)
A2= numpy.random.normal(1,1,50).reshape(25, 2)
A3= numpy.random.normal(1,1,50).reshape(25, 2)
A4= numpy.random.normal(1,1,50).reshape(25, 2)
A5= numpy.random.normal(1,1,50).reshape(25, 2)
A = numpy.dstack((A1,A2,A3,A4,A5))

B1= numpy.random.normal(3,1,50).reshape(25, 2)
B2= numpy.random.normal(3,1,50).reshape(25, 2)
B3= numpy.random.normal(3,1,50).reshape(25, 2)
B4= numpy.random.normal(3,1,50).reshape(25, 2)
B5= numpy.random.normal(3,1,50).reshape(25, 2)
B = numpy.dstack((B1,B2,B3,B4,B5))

ttest_result = stats.ttest_ind(A,B,axis=2)

但是,无法为 Mann-Witney-U 测试指定轴(如 stats.mannwhitneyu(A1,B1,axis=2)

是否有可能为 3D 数组的指定轴运行 mannwhitneyu? 由于 mannwhitneyu() 需要两个数组,因此 numpy.apply_along_axis() 也不能直接使用。有什么建议?

【问题讨论】:

    标签: python arrays numpy statistics scipy


    【解决方案1】:

    如果你想在这个例子中对第 3 轴进行类似的操作,我认为你可以这样做:

    In [304]:
    
    result = map(stats.mannwhitneyu, A.reshape(-1, A.shape[2]),B.reshape(-1, B.shape[2]))
    u = np.array([item[0] for item in result]).reshape(A.shape[0], -1)
    p = np.array([item[1] for item in result]).reshape(A.shape[0], -1)
    In [305]:
    
    u
    Out[305]:
    array([[ 0.,  4.],
           ...
           [ 1.,  2.]])
    In [306]:
    
    p
    Out[306]:
    array([[ 0.00609289,  0.04734647],
           ...
           [ 0.01078587,  0.01835693]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多