handloomweaver
handloomweaver

Reputation: 5011

Mask a 2D Numpy Array by array of indexes like np.in1d for 2d arrays

    np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,9,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,10,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,12,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,11],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

I am trying to find how to take a numpy array like above and then in one performant operation mask it with indexes of elements I want zeroed

[0,1] [1,4] [4,7] [7,8] [8,11]

So what I am left with is

np.array(
[[0,0,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,0,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

Something like the functionality of np.in1d but for a 2d array? I can iterate over each element but the arrays can be truly massive so vector single operation mask would be best. Is it possible? If this is a stupid question I'm sure I'll be told!

Upvotes: 1

Views: 292

Answers (2)

igavriil
igavriil

Reputation: 1021

You can directly access these indexes in the following way

indexes = [[0,1], [1,4], [4,7], [7,8], [8,11]]
indexes =zip(*indexes)
>>[(0, 1, 4, 7, 8), (1, 4, 7, 8, 11)]
a[indexes[0], indexes[1]]=0
>>
[[ 0  0  0  2  0  0  0  0  0  0  0  0]
 [ 0  0 15  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0 18  0  0  0  0  0  0]
 [ 0  0  0  0 27  0 20  0  0  0  0  0]
 [ 0  0  0  0  0 20  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  8  0  0  0]
 [ 0  0  0  0  0  0  0 14  0 14  0  0]
 [ 0  0  0  0  0  0  0  0  0  0 25  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0 15  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  7]
 [ 0  0  0  0  0  0  0  0  0  0  0  0]]

Upvotes: 2

plonser
plonser

Reputation: 3363

I think you look for this

a = np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,9,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,10,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,12,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,11],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

b = np.array([[0,1],[1,4],[4,7],[7,8],[8,11]])

# get x coordinates in an array
c1 = b[:,0]
# get y coordinates in an array
c2 = b[:,1]
a[c1[:,None],c2] = 0

a 
array([[ 0,  0,  0,  2,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0, 18,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0, 27,  0, 20,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0, 20,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  8,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0, 14,  0, 14,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 25,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 15,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  7],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]])

Upvotes: 1

Related Questions