Reputation: 359
l have the following 4D numpy array called my_data.
my_data=dim(8000,2350,143,20)
l would like to compute mean on third and forth dimension (143,20). lets call the third and forth dimension a sub 2d array
3d_4d_array=dim(143,20)
then :
mean_3D_4d_array=np.mean(mean_3D_4d_array,axis=0)
mean_3D_4d_array.shape
(20,)
Expected output :
mean_data=dim(8000,2350,20)
What l have tried :
my_data=np.mean(my_data,axis=2)
my_data.shape
(9360, 256, 20)
Is my try correct ?
l'm not sure that it computes means over axis=0
on the third and forth dimension (143,20)
Thank you for your help
Upvotes: 0
Views: 1152
Reputation: 1086
Small example on how to verify if your operation returns what you expect it to return:
a = np.arange(50)
a = np.reshape(a, newshape=(1,1,10,5))
print(a)
[[[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]
[35 36 37 38 39]
[40 41 42 43 44]
[45 46 47 48 49]]]]
# Mean along axis 2
b = np.mean(a, axis=2)
print(b.shape)
print(b[0,0])
(1, 1, 5)
[22.5 23.5 24.5 25.5 26.5]
# Mean along axis 3
b = np.mean(a, axis=3)
print(b.shape)
print(b[0,0])
(1, 1, 10)
[ 2. 7. 12. 17. 22. 27. 32. 37. 42. 47.]
If you want mean along axis 3 and 4, (e.g. last 2 axes) you can apply mean operation iteratively:
b = np.mean(a, axis=2)
b = np.mean(b, axis=2)
print(b.shape)
print(b[0,0])
(1, 1)
24.5
Upvotes: 2