我有一个大小为(144、173、360)的 3D 数组。是否只取每 12 个指数的平均值?

I have an 3D array of size (144, 173, 360). Is there is taking the average of only every 12th index?

我有一个形状为 144,173, 360 的 3D 数组。我想取第一个轴(大小 144)的平均值,但只取第 12 个元素的平均值,所以只取以下的平均值:

0th, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11th index of first axis

然后取平均值:

12th, 13, 14, ... 23rd indices of the first axis, etc.

我尝试使用 :: 制作一个 for 循环以仅包含每第 12 个索引,但我是否在正确的范围内循环?输出数组的形状与原始数组相同...所以让我觉得我没有在正确的范围内循环?

array = np.random.rand(144,173,360)

# trying to take mean of every 12th index of first axis

array_mean_every_12th_index = []

for i in range(12):
    array_mean_every_12th_index.append(np.mean(array[i::12],axis=0))
array_mean_every_12th_index = np.array(array_mean_every_12th_index)

我的 for 循环可能有什么问题?我将如何取第一个轴(轴 = 0,大小 144)的每 12 个索引的平均值?

修复问题:

array = np.random.rand(144,173,360)

# trying to take mean of every 12th index of first axis

array_mean_every_12th_index = []

for i in range(12):
    array_mean_every_12th_index.append(np.mean(array[(12*i):(12*(i+1))],axis=0))
array_mean_every_12th_index = np.array(array_mean_every_12th_index)

最简单的方法可能是重塑数组:

array_mean_every_12th_index = array.reshape(-1, 12, *array.shape[1:]).mean(axis=1)

当您将这样的轴重塑为两个轴时,它允许您对每第 12 个元素 (mean(axis=0)) 或每批 12 个元素 (mean(axis=1)) 求和。你似乎想要后者。