Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate Average of n numpy arrays [duplicate]

I have 'n' numpy arrays each with shape (128,) How to get an average numpy array of shape (128,) for the list of numpy arrays. I have seen the documentation of numpy's average() and mean() which describes that the average is calculated for all the elements in a single numpy array rather than multiple or list of numpy arrays. Example

numpyArrayList = [ar1,ar2,ar3,ar4...arn]
avgNumpyArray = avg(numpyArrayList)
avgNumpyArray.shape

should give result as (128,) and this array should contain the average of all the numpy arrays

Thanks in advance

like image 984
Krishna Manohar Avatar asked Oct 15 '25 07:10

Krishna Manohar


2 Answers

I would use np.mean([ar1,ar2,ar3,ar4...arn], axis=0).

like image 80
nicoco Avatar answered Oct 17 '25 00:10

nicoco


You can achieve this by using the following code

ar = [ar1,ar2,ar3,...,arn]
r = np.mean(ar)

for axis=0 use following

r = np.mean(ar, axis=0)

for axis=1 use following

r = np.mean(ar, axis=1)
like image 25
Usman Avatar answered Oct 17 '25 00:10

Usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!