0

I am trying to manipulate numpy arrays in a nested list.

I have a nested list, and in each inner list, there are several numpy arrays.

a = [
[np.random.normal(0,1,[2,3]), np.random.normal(0,1,[4,5]), np.random.normal(0,1, [9, 10])],
[np.random.normal(0,1,[2,3]), np.random.normal(0,1,[4,5]), np.random.normal(0,1, [9, 10])],
[np.random.normal(0,1,[2,3]), np.random.normal(0,1,[4,5]), np.random.normal(0,1, [9, 10])]
]

I want to element-wise compute mean of each of the numpy arrays in each list correspondingly to get a new list b, where elements are the mean of the three np.random.normal(0,1,[2,3]), the mean of the three np.random.normal(0,1,[4,5]), etc.

My initial thought was to write down a for-loop to fetch corresponding arrays and compute mean of them and then add to a list. However, this may be a bit slow when having a large number of such arrays or inner list.

8
  • I'm confused at why you'd have a list formatted like that. Why not have a list in a transposed form to what you have, i.e. have the arrays of the same size in the same sublist? Commented Nov 30, 2017 at 2:25
  • And why are you putting Numpy arrays into a 2D list like that, rather than doing it all in a Numpy array, or at least a 1D list of arrays? Commented Nov 30, 2017 at 2:27
  • @PM2Ring the arrays are of different size, meaning they cannot be put in a numpy array. However, if he organized it as I have, it would just be a list of numpy arrays instead of a nested Python list. Commented Nov 30, 2017 at 2:28
  • As long as the arrays differ in shape there isn't much you can do. Where shapes match you can 'stack' and perform fast axis wise operations. Otherwise you are stuck with list iterations. Commented Nov 30, 2017 at 2:29
  • @Sebastian Indeed! It should be a list of 3D arrays, with the OP's 2D arrays of the same shape making up the planes of the 3D arrays. Commented Nov 30, 2017 at 2:30

1 Answer 1

2

I'm not sure if this is what you want, but here's a potential solution:

b = [np.mean(row, axis=0) for row in zip(*a)]

zip(*a) rearranges the nested list into a sensible format, so row is the list of equally sized arrays, and np.mean(row, axis=0) will get the element-wise mean along the list.

You could also turn each row into a numpy array:

b = [np.array(row).mean(axis=0) for row in zip(*a)]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!. a = [[np.array([2,4]), np.array([4,6])], [np.array([3,5]), np.array([5,7])]] then b = [np.mean(row, axis=0) for row in zip(*a)] gives the exact what supposed: [array([ 2.5, 4.5]), array([ 4.5, 6.5])], I will test it over large scale.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.