是否有更有效的方法来执行逐元素最大值使用 key ?
import numpy as np
a = np.array([-2, 2, 4, 0])
b = np.array([-3,-5, 2, 0])
c = np.array([ 1, 1, 1, 1])
mxs = np.empty((4,))
for i in range(4):
mxs[i] = max([a[i], b[i], c[i]], key=abs)
>>> mxs
array([-3., -5., 4., 1.])
不幸的是,numpy.maximum
不提供key
参数,因为能够执行类似的操作会很好:
np.maximum.reduce([a,b,c])
最佳答案
您可以使用:
arr = np.array([a,b,c])
arr[np.argmax(np.abs(arr), axis=0), np.arange(arr.shape[1])]
关于python - 带键的元素最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49510534/