简体   繁体   中英

Masking arrays using numpy

I have an array and I want to mask it such that I Keep its shape as it is ie, not to delete the masked elements.

For example in this code

input = torch.randn(2, 5)

mask = input > 0
input = input[mask]
input = input *1000000000000

print(input)

printing the input is the result of the above mathematical operation on the unmasked elements and returns a 1D array without the masked elements.

you're overwriting your original array when you do input = input[mask] . If you omit that step, you can modify the masked values in place, but keep the non-masked values as is

i = np.random.randn(2, 5)

print(i)
[[ 0.48857855  0.97799014  2.29587523 -2.37257331  1.28193921]
 [ 0.62932172  1.37433223 -1.2427145   0.31424802  1.34534568]]

mask = i> 0
i[mask] *= 1000000000000

print(i)

[[ 4.88578545e+11  9.77990142e+11  2.29587523e+12 -2.37257331e+00 1.28193921e+12]
 [ 6.29321720e+11  1.37433223e+12 -1.24271450e+00  3.14248021e+11 1.34534568e+12]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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