gpt4 book ai didi

python - 带键的元素最大值

转载 作者:行者123 更新时间:2023-12-01 02:02:07 25 4
gpt4 key购买 nike

是否有更有效的方法来执行逐元素最大值使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com