我有一个大小为
(1000, 128, 128)
的数组,其中 1000 是 128x128
的总数组。现在我想对每 10 个数组(轴 = 0)取平均值,以便得到大小为 (100,128,128)
的最终数组。我可以用 np.mean(data,axis=0)
取所有数组的平均值,但我不知道在这种情况下如何取每 10 个数组的平均值。
提前致谢。
干杯,
如果你有 1000 个元素:
import numpy as np
x = np.arange(1000)
您可以重塑形状以获得长度为 10 的块:
y = x.reshape(-1, 10)
y.shape # (100, 10)
并求和
axis=1
:
y.sum(axis=1)
# array([ 45, 145, 245, ..., 9745, 9845, 9945])
如果最后有额外的维度,本质上是一样的。
import numpy as np
x = np.arange(1000)[:, np.newaxis, np.newaxis] * np.ones((128, 128))
y = x.reshape(-1, 10, 128, 128)
res = y.sum(axis=1)
res[0] # 128x128 array of 45s
res[1] # lots of 145s, etc...
您应该能够通过使用 np.split 来实现它,然后找到围绕 axis = 1 的平均值:
sub_arrays = np.split(data, 100)
np.mean(arrays, axis=1)