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

博客围绕如何用numpy以最pythonic的方式找出数组特定列的最大值展开。作者给出示例数组,尝试用numpy.amax方法未得到预期结果,后用切片方法实现但认为并非最佳,最终解决方案是通过解包列表,用a.max(axis=0)获取最大值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]])

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.

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?

解决方案

Just unpack the list:

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

In [274]: print xmax, ymax

#10 6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值