删除数组中与布尔掩码相对应的所有元素 [英] Delete all elements in an array corresponding to Boolean mask
问题描述
我有一个布尔掩码,它以二维numpy数组(布尔数组)的形式存在
I have a Boolean mask that exists as 2-D numpy array (Boolean Array)
array([[ True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True],
[False, False, False, False, False, False, False],
[False, False, False, False, False, False, False],
[False, False, False, False, False, False, False]], dtype=bool)
我还有一个单独的二维numpy值数组,其尺寸与布尔掩码(值数组)相同
I also have a separate 2-D numpy array of values that is of the same dimensions as the Boolean mask (Values Array)
array([[ 19.189 , 23.2535, 23.1555, 23.4655, 22.6795, 20.3295, 19.7005],
[ 20.688 , 20.537 , 23.8465, 21.2265, 24.5805, 25.842 , 23.198 ],
[ 22.418 , 21.0115, 21.0355, 20.217 , 24.1275, 24.4595, 21.981 ],
[ 21.156 , 18.6195, 23.299 , 22.5535, 23.2305, 28.749 , 21.0245],
[ 21.7495, 19.614 , 20.3025, 21.706 , 22.853 , 19.623 , 16.7415],
[ 20.9715, 21.9505, 21.1895, 21.471 , 21.0445, 21.096 , 19.3295],
[ 24.3815, 26.2095, 25.3595, 22.9985, 21.586 , 23.796 , 20.375 ]])
我想做的是从值数组中删除布尔值区域中相同位置等于False
的所有元素.是否有捷径可寻?
What I would like to do is delete all elements from the the array of values where the same location in the Boolean area equals False
. Is there an easy way to do this?
此示例的期望输出是:
array([[ 19.189 , 23.2535, 23.1555, 23.4655, 22.6795, 20.3295, 19.7005],
[ 20.688 , 20.537 , 23.8465, 21.2265, 24.5805, 25.842 , 23.198 ],
[ 22.418 , 21.0115, 21.0355, 20.217 , 24.1275, 24.4595, 21.981 ],
[ 21.156 , 18.6195, 23.299 , 22.5535, 23.2305, 28.749 , 21.0245]])
在此特定示例中,所有False
值都存在于布尔数组的末尾,但并非总是如此,它们可以随机分布.因此,我需要一种从values数组中删除其中任何掩码值等于Boolean数组
In this particular example, all the False
values exist at the end of the the Boolean array, but this is not always the case and they can be randomly distributed. Therefore, I need a way of deleting any element from the values array in where the corresponding mask value equals False
in the Boolean array
推荐答案
For most purposes you could simply create a MaskedArray
which behaves as if these were "removed", that also allows to "remove" single elements from a column/row while keeping the dimensionality the same:
import numpy as np
arr = np.array([[ 19.189 , 23.2535, 23.1555, 23.4655, 22.6795, 20.3295, 19.7005],
[ 20.688 , 20.537 , 23.8465, 21.2265, 24.5805, 25.842 , 23.198 ],
[ 22.418 , 21.0115, 21.0355, 20.217 , 24.1275, 24.4595, 21.981 ],
[ 21.156 , 18.6195, 23.299 , 22.5535, 23.2305, 28.749 , 21.0245],
[ 21.7495, 19.614 , 20.3025, 21.706 , 22.853 , 19.623 , 16.7415],
[ 20.9715, 21.9505, 21.1895, 21.471 , 21.0445, 21.096 , 19.3295],
[ 24.3815, 26.2095, 25.3595, 22.9985, 21.586 , 23.796 , 20.375 ]])
mask = np.array([[ True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True],
[False, False, False, False, False, False, False],
[False, False, False, False, False, False, False],
[False, False, False, False, False, False, False]])
marr = np.ma.MaskedArray(arr, mask=~mask)
marr
赠予:
masked_array(data =
[[19.189 23.2535 23.1555 23.4655 22.6795 20.3295 19.7005]
[20.688 20.537 23.8465 21.2265 24.5805 25.842 23.198]
[22.418 21.0115 21.0355 20.217 24.1275 24.4595 21.981]
[21.156 18.6195 23.299 22.5535 23.2305 28.749 21.0245]
[-- -- -- -- -- -- --]
[-- -- -- -- -- -- --]
[-- -- -- -- -- -- --]],
mask =
[[False False False False False False False]
[False False False False False False False]
[False False False False False False False]
[False False False False False False False]
[ True True True True True True True]
[ True True True True True True True]
[ True True True True True True True]],
fill_value = 1e+20)
In this case it would be also possible to just compress all rows that contain at least one masked element with np.ma.compress_rows
:
>>> np.ma.compress_rows(marr)
array([[ 19.189 , 23.2535, 23.1555, 23.4655, 22.6795, 20.3295, 19.7005],
[ 20.688 , 20.537 , 23.8465, 21.2265, 24.5805, 25.842 , 23.198 ],
[ 22.418 , 21.0115, 21.0355, 20.217 , 24.1275, 24.4595, 21.981 ],
[ 21.156 , 18.6195, 23.299 , 22.5535, 23.2305, 28.749 , 21.0245]])
这篇关于删除数组中与布尔掩码相对应的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!