【问题标题】:How to get the mean of a 3D array over 2D?如何在 2D 上获得 3D 数组的平均值?
【发布时间】:2020-06-09 06:36:09
【问题描述】:

我有一个形状如下的数组 (7352, 128, 6)

如何在 Python 中使用 NumPy 获得第 1 维和第 2 维的“均值”?

我想要获得的“手段”的结果形状是 (1, 6)。

【问题讨论】:

  • np.mean 有一个 axis 参数,正是为此

标签: python numpy


【解决方案1】:

你可以这样做:

np.mean(x, axis=(0, 1))

不过,你得到的形状是 (6,)。

【讨论】:

  • 谢谢,这是我一直在寻找的解决方案,是的,对不起,您对形状的看法是正确的!
【解决方案2】:

numpy.mean() 带有轴参数是正确的解决方案。

import numpy as np

x = np.random.rand(7352, 128, 6)
x_mean = np.mean(x, axis=(0, 1))

print(x_mean.shape)  # -> (6,)

【讨论】:

    【解决方案3】:

    可以使用mean函数的轴参数:

    img = np.random.rand(7352,128,6)
    y_mean = img.mean(axis=0) # mean over y (row)
    x_mean = img.mean(axis=1) # mean over x (column)
    z_mean = img.mean(axis=(0,1)) # the mean you want (shape is (6,))
    

    这个post 也可能有帮助。

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 2013-08-23
      • 2021-04-22
      • 1970-01-01
      • 2016-11-20
      • 2020-01-07
      • 1970-01-01
      • 2014-05-28
      • 2019-09-28
      相关资源
      最近更新 更多