0

Suppose I have an array with with the following shape:

array([[137, 138, 139, ..., 126, 126, 125],
       [139, 139, 139, ..., 126, 126, 126],
       [140, 140, 140, ..., 127, 126, 126],
       ...,
       [132, 135, 137, ..., 136, 135, 133],
       [133, 134, 136, ..., 136, 134, 133],
       [133, 134, 134, ..., 136, 134, 133]], dtype=uint8)

My goal is to manipulate the array such that I can transform it to a binary array, where a threshold (e.g. 100) determines whether the entry is 0 or 1.

For now I just implemented nested for-loops checking the row and columns respectivley.

thres = 100

for k in range(0, len(my_array)):
    for i in range(0, len(my_array[0])):
        if(my_array[k][i] < thres):
            my_array[k][i] = 1
        else:
            my_array[k][i] = 0

It is mentioned that I am not tied to any perfromance goals. But just out of curiosity: How can I make use of numpy efficiently such that I can achieve a better performance? Since the provided array can be computed in a reasonable amount of time, the above mentioned approach works just fine. I can image that with increase of the array size the performance of the approach will become quite insufficient.

1
  • How much basic reading have you done with numpy. The fact that you are using my_array[k][i] instead of my_array[k, i] suggests that you have jumped straight into this without doing much reading. That's list style indexing, which works, but does not use the full numpy indexing power. Commented Apr 26, 2020 at 16:13

1 Answer 1

2

How about

my_array[my_array < thresh] = 1
my_array[my_array > 1] = 0

or even better if you don't mind true false

my_array <= thresh

or

my_array = (my_array < thresh).astype('uint8')

if you do.

If you have to loop on an array, do not use it. They are (much) worse than Python lists for that - try it on your example and see how much faster it is. Use a list if you loop.

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

1 Comment

Worked just fine! Thank you.

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.