I have an array A with shape (2,4,1). I want to calculate the mean of A[0] and A[1] and store both the means in A_mean. I present the current and expected outputs.
import numpy as np
A=np.array([[[1.7],
[2.8],
[3.9],
[5.2]],
[[2.1],
[8.7],
[6.9],
[4.9]]])
for i in range(0,len(A)):
A_mean=np.mean(A[i])
print(A_mean)
The current output is
5.65
The expected output is
[3.4,5.65]
A.mean((1, 2))A_meanat each iteration of your loop, instead of appending a new value. @Warkaz's answer is the best solution, but a minimal fix to your loop would have been to first initializeA_mean=[]and then useA_mean.append(np,mean(A[i])in the body of your loop.