Python 如何在numpy数组列中找到最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22129225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find max value in a numpy array column?
提问by TheGeographer
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 坐标,我最终需要每个形状的高度和宽度),所以最大 x 坐标为 10,最大 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?
建议?
采纳答案by zhangxaochen
Just unpack the list:
只需打开列表:
In [273]: xmax, ymax = a.max(axis=0)
In [274]: print xmax, ymax
#10 6

