How to get the highest element in absolute value in a numpy matrix?

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)

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.

EDIT: Just to clarify, the idea is to leverage the numpy methods to reduce down to just two values and then use the key feature of the builtin max to arbitrate, since key is not supported by the numpy methods.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)