获取numpy数组的多轴均值 [英] Getting the mean of multiple axis of a numpy array

查看:105
本文介绍了获取numpy数组的多轴均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在numpy中,有没有一种快速的方法来计算多轴平均值?我正在计算n维数组中除0轴以外的所有轴的均值.

In numpy is there a fast way of calculating the mean across multiple axis? I am calculating the mean on all but the 0 axis of an n-dimensional array.

我目前正在这样做;

for i in range(d.ndim - 1):
    d = d.mean(axis=1)

我想知道是否有不使用python循环的解决方案.

I'm wondering if there is a solution that doesn't use a python loop.

推荐答案

我的方法是对数组进行整形以展平所有更高的维度,然后在轴1上求平均值.这是您要寻找的吗?

My approach would be to reshape the array to flatten all of the higher dimensions and then run the mean on axis 1. Is this what your looking for?

In [14]: x = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])

In [16]: x.reshape((x.shape[0], -1)).mean(axis=1)
Out[16]: array([ 2.5,  6.5])

(第2步只是计算较高暗点长度的乘积)

(step 2 just calculates the product of the lengths of the higher dims)

这篇关于获取numpy数组的多轴均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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