MCPcopy Create free account
hub / github.com/numpy/numpy / compress_nd

Function compress_nd

numpy/ma/extras.py:823–863  ·  view source on GitHub ↗

Suppress slices from multiple dimensions which contain masked values. Parameters ---------- x : array_like, MaskedArray The array to operate on. If not a MaskedArray instance (or if no array elements are masked), `x` is interpreted as a MaskedArray with `mask` se

(x, axis=None)

Source from the content-addressed store, hash-verified

821
822
823def compress_nd(x, axis=None):
824 """Suppress slices from multiple dimensions which contain masked values.
825
826 Parameters
827 ----------
828 x : array_like, MaskedArray
829 The array to operate on. If not a MaskedArray instance (or if no array
830 elements are masked), `x` is interpreted as a MaskedArray with `mask`
831 set to `nomask`.
832 axis : tuple of ints or int, optional
833 Which dimensions to suppress slices from can be configured with this
834 parameter.
835 - If axis is a tuple of ints, those are the axes to suppress slices from.
836 - If axis is an int, then that is the only axis to suppress slices from.
837 - If axis is None, all axis are selected.
838
839 Returns
840 -------
841 compress_array : ndarray
842 The compressed array.
843 """
844 x = asarray(x)
845 m = getmask(x)
846 # Set axis to tuple of ints
847 if axis is None:
848 axis = tuple(range(x.ndim))
849 else:
850 axis = normalize_axis_tuple(axis, x.ndim)
851
852 # Nothing is masked: return x
853 if m is nomask or not m.any():
854 return x._data
855 # All is masked: return empty
856 if m.all():
857 return nxarray([])
858 # Filter elements through boolean indexing
859 data = x._data
860 for ax in axis:
861 axes = tuple(list(range(ax)) + list(range(ax + 1, x.ndim)))
862 data = data[(slice(None),)*ax + (~m.any(axis=axes),)]
863 return data
864
865
866def compress_rowcols(x, axis=None):

Callers 2

test_compress_ndMethod · 0.90
compress_rowcolsFunction · 0.85

Calls 5

normalize_axis_tupleFunction · 0.90
getmaskFunction · 0.85
asarrayFunction · 0.70
anyMethod · 0.45
allMethod · 0.45

Tested by 1

test_compress_ndMethod · 0.72