-1

I am a bit new to Python. I am enumerating through a large list of data, as shown below, and would like to find the mean of every line.

for index, line in enumerate (data):
    #calculate the mean

However, the lines of this particular set of data are as such:

[array([[2.3325655e-10, 2.4973504e-10],
       [1.3025138e-10, 1.3025231e-10]], dtype=float32)].

I would like to find the mean of both 2x1s separately, then the average of both means, so it outputs a single number. Thanks in advance.

1 Answers1

0
import numpy as np

data = np.random.normal(0, 1, (600, 2, 2))
mean = data.mean(axis=(1, 2))

If you are given your data as a a list of np.arrays of shape (2, 2), convert it to an array first:

data = [
    np.array(
        [[2.3325655e-10, 2.4973504e-10],
         [1.3025138e-10, 1.3025231e-10]],
        dtype=np.float32
    ),
    # ...599 more elements here
]

mean = np.array(data).mean(axis=(1, 2))
Michael Hodel
  • 2,845
  • 1
  • 5
  • 10
  • Hi. Thank you for the quick response. I tried the code, but it returned this error: ValueError: maximum supported dimension for an ndarray is 32, found 600 – chemengdev Aug 29 '22 at 19:03
  • Then your data does not have three dimensions but 600, so is not, as claimed, of shape (600, 2, 2). – Michael Hodel Aug 29 '22 at 19:07
  • But when I print (mydata.shape), that's what it returns. Am I misunderstanding what .shape does? Thank you for your help – chemengdev Aug 29 '22 at 19:34
  • 1
    Please consider posting your code and providing some data – Michael Hodel Aug 29 '22 at 19:36
  • Thank you for still answering even though I haven't been super clear. I updated the post after finding one way of obtaining my desired list, but I'm still unsure on how to calculate the means of both lists to obtain 1 number. – chemengdev Aug 29 '22 at 20:38
  • Thank you very much! It worked! Have a nice rest of your week. – chemengdev Aug 29 '22 at 21:09