Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove columns where every value is masked

I want to remove columns from a masked array where every value in the column is masked. So in the following example:

>>> import numpy as np
>>> test = np.array([[1,0,0],[0,3,0],[1,4,0]])
>>> test = np.ma.masked_equal(test,0)
>>> test
[[1 -- --]
[-- 3  --]
[1  4  --]],
>>> np.somefunction(test)
[[1  --]
 [-- 3 ]
 [1  4 ]]

what should np.somefunction() be to get the given output?

like image 519
Niek de Klein Avatar asked Mar 23 '23 01:03

Niek de Klein


1 Answers

You can use fancy indexing:

test[:, ~np.all(test.mask, axis=0)]
#masked_array(data =
# [[1 --]
# [-- 3]
# [1 4]],
#             mask =
# [[False  True]
# [ True False]
# [False False]],
#       fill_value = 0)
like image 178
Saullo G. P. Castro Avatar answered Apr 02 '23 21:04

Saullo G. P. Castro