获取意味着numpy的三维数组的2D片 [英] Get mean of 2D slice of a 3D array in numpy
本文介绍了获取意味着numpy的三维数组的2D片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个形状的numpy的数组:
I have a numpy array with a shape of:
(11L, 5L, 5L)
我想在每个阵列的'片'的25个元素,以计算平均值[0,:,:],[1,:,:]等,返回11的值
I want to calculate the mean over the 25 elements of each 'slice' of the array [0, :, :], [1, :, :] etc, returning 11 values.
这似乎很傻,但我不知道如何做到这一点。我想过了均值(轴= X)
函数将这样做,但我已经试过轴的所有可能的组合,其中没有给我我想要的结果。
It seems silly, but I can't work out how to do this. I've thought the mean(axis=x)
function would do this, but I've tried all possible combinations of axis and none of them give me the result I want.
我可以明显地做到这一点使用一个for循环和切片,但肯定有一个更好的办法?
I can obviously do this using a for loop and slicing, but surely there is a better way?
推荐答案
使用一个元组轴:
>>> a = np.arange(11*5*5).reshape(11,5,5)
>>> a.mean(axis=(1,2))
array([ 12., 37., 62., 87., 112., 137., 162., 187., 212.,
237., 262.])
编辑:这仅适用于numpy的版本1.7 +
This works only with numpy version 1.7+.
这篇关于获取意味着numpy的三维数组的2D片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文