1

I have a 3d with about 8e6 elements, and i need to change all of the elements. I want to do something like this

def func(x, index):
....


flat = array.reshape(-1)
flat[flat == 9999] = 0
flat[flat >= 0.2 and flat < 0.7] = func(flat, 0)
flat[flat >= 0.7 and flat < 1.5] = func(flat, 1)
flat[flat >= 0.7 and flat < 1.5] = func(flat, 2)
....
...

This code doesnt work. I've tried np.nditer but it wouldnt let me retrieve the value per index, it seems to be retrieving the whole array. in short of looping w/ a for loop per dimension and read and write each value, is there another way i can do this?

Thanks

0

2 Answers 2

4

While using multiple conditionals for masking, you need to use bitwise operator &:

flat[(flat >= 0.2) & (flat < 0.7)] = ...

Additionally, as your mask dimensions and the result of the func might not be the same (looks to me that func does apply something to the whole array), you should do something like:

mask = (flat >= 0.2) & (flat < 0.7)
flat[mask] = func(flat, 0)[mask]

Or like this if your func is elementwise:

mask = (flat >= 0.2) & (flat < 0.7)
flat[mask] = func(flat[mask], 0)

Then repeat the same process for the other 2 conditions.


As a bonus, you can iterate over all your lower bounds ranges and do like this:

lower_bound = [0.2, 0.7, 1.5]
upper_bound = lower_bound[1:] + [np.inf]

for i in range(len(lower_bound)):
    mask = (flat >= lower_bound[i]) & (flat < upper_bound[i])
    flat[mask] = func(flat[mask], i)

That would avoid you of writing 100 lines if you have 100 ranges.

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

1 Comment

thanks that works. as of now i would need 10 masks is there a better way than creating 10 masks?
2

The syntax is flat[(flat >= 0.2) & (flat < 0.7)] and similar. Note the right hand side of flat[--] = func(--)has to have the same number of elements, so you want call func on the indexed/masked array.

There are some NumPy functions that can help with this kind of code:

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.