1

I just learned Python, I want to ask something..

For example, I have

import numpy as np

a = np.array([[11, 12, 13],
              [14, 16, 13],
              [17, 15, 11],
              [12, 14, 15]])

I want to find the label of that array

so .. in the first row the max value is 13, then the label result = 3

in the second row the max value is 16, then the label result = 2

desired results is like this

[3 2 1 3] or [[3]
              [2]
              [1]
              [3]]
0

2 Answers 2

1

You can try this:

>>> import numpy as np

>>> a = np.array([[11, 12, 13],
              [14, 16, 13],
              [17, 15, 11],
              [12, 14, 15]])

>>> np.argmax(a, axis=1) + 1

array([3, 2, 1, 3], dtype=int64)

np.argmax gives the indices of the max values in the specified axis. So,

>>> np.argmax(a, axis=1)
array([2, 1, 0, 2], dtype=int64)

Then all you need to do is add 1 to it.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it this way.

d=[]
for lst in a:
    d.append(lst.index(max(lst))+1)
print(d)

output

[3, 2, 1, 3]

d=[]
for lst in a:
    d.append([lst.index(max(lst))+1])
print(d)

output

[[3], [2], [1], [3]]

Comments

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.