python - Delete all elements in an array corresponding to Boolean mask -
i have boolean mask exists 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)
i have separate 2-d numpy array of values of same dimensions 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 ]])
what delete elements the array of values same location in boolean area equals false
. there easy way this?
the desired output example is:
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]])
in particular example, false
values exist @ end of the boolean array, not case , can randomly distributed. therefore, need way of deleting element values array in corresponding mask value equals false
in boolean array
for purposes create maskedarray
behaves if these "removed", allows "remove" single elements column/row while keeping dimensionality same:
import numpy 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
gives:
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 case possible compress rows contain @ least 1 masked element 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]])
Comments
Post a Comment