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

aru*_*run 4 python numpy

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

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

s = numpy.argmax(numpy.abs(X),axis=0) 
Run Code Online (Sandbox Code Playgroud)

给了我需要的元素的索引s = [ 0,1,1].

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

Bi *_*ico 6

试试这个:

# 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])]
Run Code Online (Sandbox Code Playgroud)