1

I am trying to convert a NumPy array of probabilities that sum up to 1.0, into a binary array. Essentially, I want to turn the max probability in the array into 1, and the rest into 0s. Of course, I know how to do this using Python, but I was hoping that NumPy or SciPy already come up with a built-in function for making the operation more performant (we are speaking about a matrix containing millions of such arrays).

2
  • Show your python code? Commented May 19, 2018 at 8:27
  • Please provide a small example to clarify. Commented May 19, 2018 at 8:33

1 Answer 1

2

Compare items with the maximum value, and cast to int

>>> a=np.array([0.435,0.24,0.241,0.13,0.56])

>>> np.int8( a == a.max() )
array([0, 0, 0, 0, 1], dtype=int8)
Sign up to request clarification or add additional context in comments.

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.