gpt4 book ai didi

python - 从 numpy.argmax 结果中提取 numpy 数组切片

转载 作者:太空狗 更新时间:2023-10-30 02:21:45 25 4
gpt4 key购买 nike

我有一个 (3,3) numpy 数组,想找出绝对值的元素的符号值为最大值:

X = [[-2.1,  2,  3],
[ 1, -6.1, 5],
[ 0, 1, 1]]

s = numpy.argmax(numpy.abs(X),axis=0)

给我所需元素的索引,s = [0,1,1]

我如何使用此数组提取元素 [ -2.1, -6.1, 5] 以找出它们的符号?

最佳答案

试试这个:

# You might need to do this to get X as an ndarray (for example if X is a list)
X = numpy.asarray(X)

# Then you can simply do
X[s, [0, 1, 2]]

# Or more generally
X_argmax = X[s, numpy.arange(X.shape[1])]

关于python - 从 numpy.argmax 结果中提取 numpy 数组切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14222110/

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