4

我有一个 (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]以找出它们的符号?

4

2 回答 2

7

试试这个:

# 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])]
于 2013-01-08T19:06:27.267 回答
0

部分答案:使用signor signbit

In [8]: x = numpy.array([-2.1, -6.1, 5])

In [9]: numpy.sign(x)
Out[9]: array([-1., -1.,  1.])

In [10]: numpy.signbit(x)
Out[10]: array([ True,  True, False], dtype=bool)
于 2013-01-08T19:21:11.040 回答