简体   繁体   中英

Masking an array

I am trying to mask an array (called dataset) in python: The array has the following size (5032, 48, 48). Basically these are 5032 48x48 images. But some of the images may not contain any data, so there might only be 0's there. These are the ones I want to mask.

I tried the following: (dataset[:] == 0).all(axis=0). When I print the shape of the above operation I get (5032, 48) which is not what I want. I expected (5032, ).

I am not sure what I am doing wrong. I wanted to create a mask with the size (5032, ) which has True (if there is at least one value in the 48x48 array that is nonzero) and False (if there are only zero values in the 48x48 array) values.

Thanks for your help

Kind of a hacky way, but just sum across the last two axis and check if the sum is zero.

nonzero_images = images[np.where(np.sum(images, axis = (1, 2)) == 0)]

You can try something like

# sample data - 3 nonzeros and 2 zeros
dataset = np.concatenate([np.ones((3, 48, 48)), np.zeros((2, 48, 48))])
new = dataset[np.unique(np.where(dataset.all(axis=1))[0])]

print(f'Dataset Shape: {dataset.shape}\nNew Shape: {new.shape}')
# Dataset Shape: (5, 48, 48)
# New Shape: (3, 48, 48)

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