简体   繁体   English

应用相同形状的掩码时,Numpy 数组会丢失维度

[英]Numpy array losing dimensions when applying mask of same shape

I have created a 2D mask appropriately named mask to have the same shape as the array data , the array I want to apply it upon.我已经创建了一个2D掩模适当命名mask为具有相同的形状,该阵列data ,我想在应用它的阵列。 However when I do so, the data loses it's shape and becomes 1D.但是,当我这样做时,数据会失去它的形状并变成一维。

I thought that as each level for axis 0 is identical (shown with creation of mask using loop comprehension) that the output would produce output with shape (837, 10)我认为由于轴 0 的每个级别都是相同的(显示为使用循环理解创建mask ),因此输出将产生形状为(837, 10)输出

I am wondering is there any numpy tricks to be used to achieve this goal without using reshape?我想知道是否有任何 numpy 技巧可用于在不使用 reshape 的情况下实现此目标?

>>> data.shape
(837, 44)

>>> m = altitudes < 50000
>>> m.shape
(44,)

>>> np.sum(m) # calculates my expected dimension for axis 1
10

>>> mask = [m for i in range(data.shape[0])]
>>> mask.shape
(837, 44)

>>> new_data = data[mask]
>>> new_data.shape
(8370,) # same as 837 * 10 (dimension wanted)

If this can't be achieved, why would this be?如果这不能实现,为什么会这样?

The 'correct' way of achieving your goal is to not expand the mask to 2D.实现目标的“正确”方法是不要将蒙版扩展到 2D。 Instead index with [:, mask] with the 1D mask.而是用[:, mask]索引和一维掩码。 This indicates to numpy that you want axis 0 unchanged and mask applied along axis 1.这向 numpy 表明您希望轴 0 保持不变并沿轴 1 应用mask

a = np.arange(12).reshape(3, 4)
b = np.array((1,0,1,0),'?')
a
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]])
b
# array([ True, False,  True, False])
a[:, b]
# array([[ 0,  2],
#        [ 4,  6],
#        [ 8, 10]])

If your mask is already 2D, numpy won't check whether all its rows are the same because that would be inefficient.如果你的mask已经是 2D 的,numpy 将不会检查它的所有行是否都相同,因为那样效率很低。 But obviously you can use [:, mask[0]] in that case.但显然你可以在这种情况下使用[:, mask[0]]

If your mask is 2D and just happens to have the same number of True s in each row then either use @tel's answer.如果您的mask是 2D 并且恰好在每行中具有相同数量的True s,那么要么使用@tel 的答案。 Or create an index array:或者创建一个索引数组:

B = b^b[:3, None]
B
# array([[False,  True, False,  True],
#        [ True, False,  True, False],
#        [False,  True, False,  True]])
J = np.where(B)[1].reshape(len(B), -1)

And now either现在要么

np.take_along_axis(a, J, 1)
# array([[ 1,  3],
#        [ 4,  6],
#        [ 9, 11]])

or或者

I = np.arange(len(J))[:, None]
IJ = I, J
a[IJ]
# #array([[ 1,  3],
#         [ 4,  6],
#         [ 9, 11]])

I believe what you want can be done by calling new_data.reshape(837, -1) .我相信您可以通过调用new_data.reshape(837, -1)来完成您想要的操作。 Here's a brief example:下面是一个简短的例子:

arr = np.arange(8*6).reshape(8,6)
maskpiece = np.array([True, False]*3)
mask = np.broadcast_to(maskpiece, (8,6))

print('the original array\n%s\n' % arr)
print('the flat masked array\n%s\n' % arr[mask])
print('the masked array reshaped into 2D\n%s\n' % arr[mask].reshape(8, -1))

Output:输出:

the original array
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]
 [36 37 38 39 40 41]
 [42 43 44 45 46 47]]

the flat masked array
[ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46]

the masked array reshaped into 2D
[[ 0  2  4]
 [ 6  8 10]
 [12 14 16]
 [18 20 22]
 [24 26 28]
 [30 32 34]
 [36 38 40]
 [42 44 46]]

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

相关问题 相同形状数组的掩码 - Mask for an array in the same shape 使用相同形状的现有掩码屏蔽 numpy 数组 - masking a numpy array using an existing mask of the same shape 在嵌套的numpy数组上应用蒙版-numpy-python - applying a mask on a nested numpy array - numpy - python 将 numpy 数组维度扩展为给定形状 - Expand numpy array dimensions to a given shape 检测 numpy 阵列的维数(不是形状) - Detect number of dimensions of numpy array (not shape) 如何将 NumPy 多维数组重塑为另一个维度相同但形状不同的数组? - How do I reshape a NumPy multi dimensional array to another array with the same dimensions, but different shape? 在循环numpy数组时返回具有相同尺寸的子数组 - returning subarrays with the same dimensions when looping over numpy array Numpy:为 arrays 数组应用 boolean 掩码时,最有效的方法是记录原始 ZA3CBC93F9D0CE2DE7D1C1 中的项目 - Numpy: When applying a boolean mask for an array of arrays, most efficient way to record which items were in the original arrays 当它们转换为Numpy数组时,如何访问Tiff中的Z轴? 形状只有二维 - How do you access the Z axis in Tiff's when they are converted to a numpy array? The shape is only in 2 dimensions 为什么在 cython 函数中请求 numpy 数组的形状时我得到 8 个维度? - Why when requesting the shape of a numpy array within a cython function I get 8 dimensions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM