Home > front end >  how to get elements in sub-array of a numpy array in python
how to get elements in sub-array of a numpy array in python

Time:01-20

I'm getting in trouble with reading the entries of a sub-array of a numpy array, in python. I've got something like this:

a = np.array([ [453,254,[1,2,3,4,5]], [743,251,[10,20,30,40,50]], [127,393,[11,22,33,44,55]] ], dtype=object)

and I need to calculate the average value of each nth column of the 2nd-position sub-array, i.e. np.mean([1,10,11]), np.mean([2,20,22]), etc.

How can I get the [1,10,11], [2,20,22], etc. sub-arrays?

I've tried different combinations with ":" and "," but I can't figure it out. I also put dtype=object in the numpy array definition, but it doesn't make any difference. Thanks in advance.

CodePudding user response:

You can do something like this:

np.mean([x[0] for x in a[:,2]])
np.mean([x[1] for x in a[:,2]])
...

CodePudding user response:

I have found a possible solution using

np.dstack((a[:,2]))[0][0]
  •  Tags:  
  • Related