简体   繁体   English

如何在 2D 上获得 3D 数组的平均值?

[英]How to get the mean of a 3D array over 2D?

I have an array with the following shape (7352, 128, 6)我有一个具有以下形状的数组 (7352, 128, 6)

How can I get the "mean" of the 1st and 2nd dimensions with NumPy in Python?如何在 Python 中使用 NumPy 获得第一维和第二维的“平均值”?

The resulting shape of the "means" I want to obtain is (1, 6).我想要获得的“手段”的结果形状是 (1, 6)。

You can do:你可以做:

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

You resulting shape would be (6,) though.你得到的形状将是 (6,)。

numpy.mean() with axis argument is proper solution. 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,)

You can use the axis parameter of the mean function:您可以使用均值函数的轴参数:

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,))

This post may also help.这篇文章也可能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM