2

I'm trying to do something along the lines of a masked broadcast, where only certain values are broadcasted.

Suppose I have one bigger array, bigger_array, and one smaller array, smaller_array:

import numpy as np
import numpy.ma as ma

bigger_array = np.zeros((4,4), dtype=np.int32)
smaller_array = np.ones((2,2), dtype=np.int32)

Now, I only want the first three values of the smaller array to replace those of a certain section of the bigger array, but masking doesn't do what I'd hoped it would do:

masked_smaller_array = ma.masked_array(smaller_array, mask=[(0, 0), (0, 1)])
bigger_array[2:4, 2:4] = masked_smaller_array 

This just returns the same thing a regular broadcast would, namely:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 1 1]
 [0 0 1 1]]

Instead of my hoped for

[[0 0 0 0]
 [0 0 0 0]
 [0 0 1 1]
 [0 0 1 0]]

Stripping out the masked value before overriding via

bigger_array[2:4, 2:4] = masked_smaller_array[~masked_smaller_array.mask]

is also of no use as that flattens the array making broadcasting incompatible.

Is there some other way to achieve the same effect perhaps?

2 Answers 2

1

You were close when you had -

bigger_array[2:4, 2:4] = masked_smaller_array[~masked_smaller_array.mask]

You were just needed to index the left side of the equation to use the same mask over there as well. Thus, one way to solve it would be to fix it, like so -

# Mask corresponding to smaller array from where elements are to be taken
select_mask = ~masked_smaller_array.mask

# Use the mask on source (smaller array) to select specific elements from it 
# and update sliced and masked (with same mask) places in bigger array
bigger_array[2:4, 2:4][select_mask] = smaller_array[select_mask]

Sample run -

In [59]: bigger_array = np.zeros((4,4), dtype=np.int32)
    ...: smaller_array = np.ones((2,2), dtype=np.int32)
    ...: masked_smaller_array =ma.masked_array(smaller_array,mask=[(0, 0),(0, 1)])
    ...: 

In [60]: select_mask = ~masked_smaller_array.mask

In [61]: select_mask
Out[61]: 
array([[ True,  True],
       [ True, False]], dtype=bool)

In [62]: bigger_array[2:4, 2:4][select_mask] = smaller_array[select_mask]

In [63]: bigger_array
Out[63]: 
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 1, 1],
       [0, 0, 1, 0]], dtype=int32)
Sign up to request clarification or add additional context in comments.

Comments

0

Actually could use numpy.where with your mask as condition and fill it with smaller_array when condition is true. Btw IIUC you could use make_mask instead of masked_array for that purpose:

your_mask = [(0, 0), (0, 1)]
mask = ma.make_mask(your_mask)
np.where(~mask, smaller_array, bigger_array[2:4, 2:4])


In [106]: mask
Out[106]: 
array([[False, False],
       [False,  True]], dtype=bool)

In [108]: np.where(~mask, smaller_array, bigger_array[2:4, 2:4])
Out[108]: 
array([[1, 1],
       [1, 0]], dtype=int32)

Then you could assign that for your variable

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.