4

I have a (3,3) numpy array and would like to figure out the sign of the element whose absolute value is maximum:

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

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

gives me the indices of the elements that I need, s = [ 0,1,1].

How can I use this array to extract the elements [ -2.1, -6.1, 5] to figure out their sign?

2 Answers 2

7

Try this:

# 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])]
Sign up to request clarification or add additional context in comments.

Comments

0

Partial answer: Usesign or 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)

1 Comment

I was a more interested in extracting the actual elements - Bi Rico's answer. I knew I could use numpy.sign on the result. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.