0

I have a 3D array of image such as

[
    [
        [225, 0, 0],
        [225, 225, 0],
        ...
    ],
    [
        [225, 0, 0],
        [225, 225, 0],
        ...
    ],
    ...
]

The size of this array is 500x500x3 which is 750.000 elements. These are simple nested loops to iterate over the array

for row in arr:
    for col in row:
        for elem in col:
            elem = (2 * elem / MAX_COLOR_VAL) - 1

But it takes a lot of time (> 5 min) to iterate.

I'm new in numpy so may be I'm iterating arrays wrong way? How can I optimize these loops?

1
  • 3
    You should not iterate over a numpy array. In fact it is very likely that iterating over a numpy array will be slower than iterating over a Python list. The idea of a numpy array is to do operations in bulk. Your program will not even set the items anyway. Commented Sep 26, 2019 at 16:57

1 Answer 1

1

Numpy arrays are not designed to do iteration over the elements. Likely it will even be slower than iterating over a Python list, since that will result in a lot of wrapping and unwrapping of elements.

Numpy arrays are designed to do processing in bulk. So for example calculate the elementwise-sum of two 1000×1000 matrices.

If you want to multiply all elements with 2, divide these by MAX_COLOR_VAL and subtract one from these, you can simply construct a new array with:

arr = (2 * arr.astype(float) / MAX_COLOR_VAL) - 1

This will apply this operation to all elements.

Note: note that if you iterate over a numpy array, you do not iterate over the indices, you iterate over the rows itself. So the row in for row in arr will return a 2d array, not the index of a 2d array.

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

2 Comments

I think casting the array to float (arr.astype(float)) is bit more expensive than just doing something like 2.0 * arr / MAX_COLOR_VAL) - 1. Why is this explicit casting necessary?
@kmario23: when I run this 10'000 times with timeit, the .astype(float) gives 3.6568590000097174, whereas the 2.0 * ... gives 3.686493999994127. I think it does not matter much. As a functional programmer. I do not really like this float times int idea, since it reminds too much of weakly typed languages like C#. As Python says: "explicit over implicit" :)

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.