Removing entries from a numpy array

I have a numpy multidimensional array with the form (4, 2000). Each column in the array represents a 4D element, where the first two elements represent two-dimensional positions.

Now I have an image mask with the same shape as the image, which is binary, and tells me which pixels are valid or invalid. Record 0 in the mask selects invalid pixels.

Now I would like to do this is a filter of my first array based on this mask, that is, delete entries in which position elements in my first array correspond to invalid image pixels. This can be done by looking at the corresponding entries in the mask and marking the deleted columns corresponding to the value 0 in the mask.

So something like:

import numpy as np
# Let mask be a 2D array of 0 and 1s

array = np.random.rand(4, 2000)

for i in range(2000):
    current = array[:, i]
    if mask[current[0], current[1]] <= 0:
        # Somehow remove this entry from my array.

, , .

+4
2

x y array :

xarr, yarr = array[0, :], array[1, :]

(2000), True, 1:

idx = mask[xarr, yarr].astype(bool)

mask[xarr, yarr] " " . , ith idx mask[xarr[i], yarr[i]].

array:

result = array[:, idx]

import numpy as np

mask = np.random.randint(2, size=(500,500))
array = np.random.randint(500, size=(4, 2000))

xarr, yarr = array[0, :], array[1, :]
idx = mask[xarr, yarr].astype(bool)
result = array[:, idx]

cols = []
for i in range(2000):
    current = array[:, i]
    if mask[current[0], current[1]] > 0:
        cols.append(i)
expected = array[:, cols]

assert np.allclose(result, expected)
+3

, . !

2 , . , , .

import numpy.ma as ma
a = ma.array((([[1,2,3,4,5],[6,7,8,9,10]]),mask=[[0,0,0,1,0],[0,0,1,0,0]])
a[:,-a.mask.any(0)] # this is where the action happens

a.mask.any(0) , . ( "-" ), , .

:

[[1 2 5],[6 7 10]]

, . , .

0

All Articles