Scipy计算Mann Whitney U沿多个切片 [英] Scipy Compute Mann Whitney U along Multiple Slices

查看:53
本文介绍了Scipy计算Mann Whitney U沿多个切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下数组:

import numpy as np
from scipy.stats import mannwhitneyu

s1 = np.array([[1,2,3,4,5,6,7,8,0,10],[10,9,8,7,6,5,4,3,2,1]])
s2 = np.array([[1,11,3,7,5,6,7,8,0,10],[10,9,8,7,6,15,4,13,2,1]])

我想对各个样本的每个切片运行一次Mann-Whitney(-Wilcoxon)U检验,并将结果填充到一个输出数组中,其中一个切片用于U统计量,另一个切片用于p值.我知道我可以这样单独运行它们:

I want to run the Mann-Whitney(-Wilcoxon) U test once for each slice of the respective samples and have the results populate into one output array with one slice for the U statistic and the other for the p-value. I know I can run them individually like this:

r1 = mannwhitneyu(s1[0], s2[0])
r2 = mannwhitneyu(s1[1], s2[1])

输出:

MannwhitneyuResult(statistic=39.5, pvalue=0.2239039981060696)
MannwhitneyuResult(statistic=37.0, pvalue=0.17162432050520815)

所需的输出:

array([39.5, 0.2239039981060696], [ 37.0, 0.17162432050520815])

我已经尝试过 np.apply_along_axis ,但是array参数仅需要一个输入,而我只有2个输入.另外,我需要最快的解决方案,因为在仿真过程中,我将在数千个切片中执行此操作.

I have tried np.apply_along_axis but the array argument only takes one input and I have 2. Also, I need the fastest solution possible as I'll be doing this over thousands of slices as part of a simulation.

提前谢谢!

推荐答案

您可以使用 map(...),它是最佳选择,并且比 np.apply_along_axis更快.(...),因为它使用

You could use map(...), is the best choice, and quite faster than, np.apply_along_axis(...), as it uses a python loop internally, and some of a computationally expensive ops i.e. transpose(...) and view(...), so under usual circumstances even looping through Numpy array using python loop, would be faster.

import numpy as np
from scipy.stats import mannwhitneyu

s1 = np.array([[1,2,3,4,5,6,7,8,0,10],[10,9,8,7,6,5,4,3,2,1]])
s2 = np.array([[1,11,3,7,5,6,7,8,0,10],[10,9,8,7,6,15,4,13,2,1]])

idx = np.arange(len(s1))


def step(i):

  return [*mannwhitneyu(s1[i], s2[i])]


np.array(list(map(step, idx)))

这篇关于Scipy计算Mann Whitney U沿多个切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆