如何在numpy矩阵中获得绝对值最高的元素? [英] How to get the highest element in absolute value in a numpy matrix?

查看:1263
本文介绍了如何在numpy矩阵中获得绝对值最高的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前正在做的事情,它可以工作,但是有点麻烦:

Here is what I am currently doing, it works but it's a little cumbersome:

x = np.matrix([[1, 1], [2, -3]])
xmax = x.flat[abs(x).argmax()]

推荐答案

您要查找的值必须为x.max()x.min(),这样您就可以做到

The value you're looking for has to be either x.max() or x.min() so you could do

max(x.min(), x.max(), key=abs)

这与aestrivex的解决方案相似,但更具可读性?请注意,如果x.min()x.max()具有相同的绝对值,例如,这将返回最小值. -55.如果您有偏好,只需将输入顺序排序为max.

which is similar to aestrivex's solution but perhaps more readable? Note this will return the minimum in the case where x.min() and x.max() have the same absolute value e.g. -5 and 5. If you have a preference just order the inputs to max accordingly.

这篇关于如何在numpy矩阵中获得绝对值最高的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆