如何在numpy数组列中找到最大值?

时间:2022-08-24 07:31:05

I can find quite a few permutations of this question, but not this (rather simple) one: how do I find the maximum value of a specific column of a numpy array (in the most pythonic way)?

我可以找到这个问题的相当多的排列,但不是这个(相当简单)的一个:如何找到numpy数组的特定列的最大值(以最pythonic的方式)?

a = array([[10, 2], [3, 4], [5, 6]])

What I want is the max value in the first column and second column (these are x,y coordinates and I eventually need the height and width of each shape), so max x coordinate is 10 and max y coordinate is 6.

我想要的是第一列和第二列中的最大值(这些是x,y坐标,我最终需要每个形状的高度和宽度),因此max x坐标为10,max y坐标为6。

I've tried:

我试过了:

xmax = numpy.amax(a,axis=0)
ymax = numpy.amax(a,axis=1)

but these yield

但这些产量

array([10, 6])
array([10, 4, 6])

...not what I expected.

......不是我的预期。

My solution is to use slices:

我的解决方案是使用切片:

xmax = numpy.max(a[:,0])
ymax = numpy.max(a[:,1])

Which works but doesn't seem to the best approach.

哪个有效,但似乎不是最好的方法。

Suggestions?

建议?

1 个解决方案

#1


31  

Just unpack the list:

只需解压缩列表:

In [273]: xmax, ymax = a.max(axis=0)

In [274]: print xmax, ymax
#10 6

#1


31  

Just unpack the list:

只需解压缩列表:

In [273]: xmax, ymax = a.max(axis=0)

In [274]: print xmax, ymax
#10 6