2

How can I select columns based on vector applied to matrix? Extension: How to apply mask from array to another matrix in numpy

ar2 = np.arange(1,26)[::-1].reshape([5,5]).T
ar3 = np.array([1,2,3,1,2])
print (ar2, '\n\n',  ar3)

results in:

[[25 20 15 10  5]
 [24 19 14  9  4]
 [23 18 13  8  3]
 [22 17 12  7  2]
 [21 16 11  6  1]] 

 [1 2 3 1 2]

What I am after is:

1  [[ 25  np.nan np.nan np.nan np.nan]
2   [ 24   19    np.nan np.nan np.nan]
3   [ 23   18      13   np.nan np.nan]
1   [ 22  np.nan np.nan np.nan np.nan]
2   [ 21   16    np.nan np.nan np.nan ]] 

1 Answer 1

2

We can leverage broadcasting to create the mask with a ranged array comparison against ar3 for assigning into those places and then assign NaNs. Since, the input is an int array, we need to make a float copy of ar2 and then assign, like so -

out = ar2.astype(float, copy=True) # convert to float as NaNs are to be assigned
mask = ar3[:,None] <= np.arange(ar2.shape[1])
out[mask] = np.nan

For a case with a large number of rows and a decent number of cols, this should be a good method, otherwise slice into each row and assign NaNs limited by the corresponding ar3 values.


Bit more explanation on the mask creation -

In [38]: ar3
Out[38]: array([1, 2, 3, 1, 2])

In [39]: ar3[:,None] <= np.arange(ar2.shape[1])
Out[39]: 
array([[False,  True,  True,  True,  True],
       [False, False,  True,  True,  True],
       [False, False, False,  True,  True],
       [False,  True,  True,  True,  True],
       [False, False,  True,  True,  True]])

Comparing each element of ar3 with the range(5) with that outer comparison gives us each row of the mask. If we look closely it's all False until that corresponding index value (ar3 value) and True thereafter. We need those True places for assigning NaNs and hence, this mask directly helps us in assgning NaNs in the entire output array.

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.