简体   繁体   English

从numpy数组中删除条目

[英]removing entries from a numpy array

I have a multidimensional numpy array with the shape (4, 2000). 我有一个多维的numpy数组形状(4,2000)。 Each column in the array is a 4D element where the first two elements represent 2D positions. 数组中的每一列都是4D元素,其中前两个元素表示2D位置。

Now, I have an image mask with the same shape as an image which is binary and tells me which pixels are valid or invalid. 现在,我有一个与二进制图像形状相同的图像蒙版,告诉我哪些像素有效或无效。 An entry of 0 in the mask highlights pixels that are invalid. 掩码中的条目0突出显示无效的像素。

Now, I would like to do is filter my first array based on this mask ie remove entries where the position elements in my first array correspond to invalid pixels in the image. 现在,我想做的是基于这个掩码过滤我的第一个数组,即删除第一个数组中的位置元素对应于图像中的无效像素的条目。 This can be done by looking up the corresponding entries in the mask and marking those columns to be deleted which correspond to a 0 entry in the mask. 这可以通过查找掩码中的相应条目并标记要删除的那些列来完成,这些列对应于掩码中的0条目。

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.

If possible, I would like to do this without looping as I have in my incomplete code. 如果可能的话,我想在没有循环的情况下这样做,因为我的代码不完整。

You could select the x and y coordinates from array like this: 您可以从array选择x和y坐标,如下所示:

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

Then form a boolean array of shape (2000,) which is True wherever the mask is 1: 然后形成一个布尔形状的数组(2000,),无论掩码是1,它都是True:

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

mask[xarr, yarr] is using so-called "integer array indexing" . mask[xarr, yarr]正在使用所谓的“整数数组索引” All it means here is that the ith element of idx equals mask[xarr[i], yarr[i]] . 这意味着idxith元素等于mask[xarr[i], yarr[i]]

Then select those columns from array : 然后从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)

I'm not sure if I'm reading the question right. 我不确定我是否正确地阅读了这个问题。 Let's try again! 让我们再试一次!

You have an array with 2 dimensions and you want to remove all columns that have masked data. 您有一个包含2维的数组,并且您希望删除所有具有屏蔽数据的列。 Again, apologies if I've read this wrong. 如果我读错了,请再次道歉。

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

the a.mask.any(0) identifies all columns that are masked into a Boolean array. a.mask.any(0)标识所有被屏蔽到布尔数组中的列。 It's negated (the '-' sign) because we want the inverse, and then it uses that array to remove all masked values via indexing. 它被否定(' - '符号),因为我们想要反转,然后它使用该数组通过索引删除所有被屏蔽的值。

This gives me an array: 这给了我一个数组:

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

In other words, the array has all removed all columns with masked data anywhere. 换句话说,数组已经删除了所有具有屏蔽数据的列。 Hope I got it right this time. 希望这次我做对了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM