高效的二维numpy数组统计

高效的二维numpy数组统计

本文介绍了高效的二维numpy数组统计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多100x100网格,是否有一种有效的方法使用numpy计算每个网格点的中值,并仅返回一个带有中值的100x100网格?目前,我正在使用for循环遍历每个网格点,计算中值,然后将它们组合成一个网格.我敢肯定有一种更好的方法来使用numpy来做到这一点.任何帮助,将不胜感激!谢谢!

I have many 100x100 grids, is there an efficient way using numpy to calculate the median for every grid point and return just one 100x100 grid with the median values? Presently, I'm using a for loop to run through each grid point, calculating the median and then combining them into one grid at the end. I'm sure there's a better way to do this using numpy. Any help would be appreciated! Thanks!

推荐答案

创建为100x100xN数组(如果不可能的话,将其堆叠在一起),并使用np.median和正确的轴一次完成该操作:

Create as 100x100xN array (or stack together if that's not possible) and use np.median with the correct axis to do it in one go:

import numpy as np
a = np.random.rand(100,100)
b = np.random.rand(100,100)
c = np.random.rand(100,100)
d = np.dstack((a,b,c))
result = np.median(d,axis=2)

这篇关于高效的二维numpy数组统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:26