简体   繁体   中英

Extremum of a weird array in numpy (python3)

I have an array that looks like this:

ar=[[[678,701]],
 [[680,702]],
 [[674,710]],
 ...]

I have to find extrema for each of the column (ie, for these 678,680,674... and for 701,702,710,... independently).

I tried to access these columns with something like this:

ar[:][0][0] or ar[:][0][1]

but it turned out, that the Python understood ar[:][0] just the same as ar[0] , and because of that I don't know any way to prevent from using loops. Are there still any sophisticated technique to do that?

To find the extrema along particular axes, you can use the axis parameter :

import numpy as np

ar = np.array([[[678,701]],
 [[680,702]],
 [[674,710]],
])

print ar.max(axis=0)  #  [[680 710]]
print ar.max(axis=0)  #  [[674 701]]

To slice out the two columns, you can use:

print ar[:,0,0]       # [678 680 674]
print ar[:,0,1]       # [701 702 710]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM