如何在numpy数组列中找到最大值? [英] How to find max value in a numpy array column?
问题描述
我可以找到这个问题的很多排列,但不是这个(相当简单)的排列:如何找到numpy数组的特定列的最大值(以最pythonic的方式)?
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)?
a = array([[10, 2], [3, 4], [5, 6]])
我想要的是第一列和第二列的最大值(这些是x,y坐标,我最终需要每种形状的高度和宽度),因此max x坐标是10,max y坐标是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.
我尝试过:
xmax = numpy.amax(a,axis=0)
ymax = numpy.amax(a,axis=1)
但是这些产量
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.
建议?
推荐答案
只需解压缩列表:
In [273]: xmax, ymax = a.max(axis=0)
In [274]: print xmax, ymax
#10 6
这篇关于如何在numpy数组列中找到最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!