MCPcopy Index your code
hub / github.com/numpy/numpy / compress_nd

Function compress_nd

numpy/ma/extras.py:841–896  ·  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

839
840
841def compress_nd(x, axis=None):
842 """Suppress slices from multiple dimensions which contain masked values.
843
844 Parameters
845 ----------
846 x : array_like, MaskedArray
847 The array to operate on. If not a MaskedArray instance (or if no array
848 elements are masked), `x` is interpreted as a MaskedArray with `mask`
849 set to `nomask`.
850 axis : tuple of ints or int, optional
851 Which dimensions to suppress slices from can be configured with this
852 parameter.
853 - If axis is a tuple of ints, those are the axes to suppress slices from.
854 - If axis is an int, then that is the only axis to suppress slices from.
855 - If axis is None, all axis are selected.
856
857 Returns
858 -------
859 compress_array : ndarray
860 The compressed array.
861
862 Examples
863 --------
864 >>> import numpy as np
865 >>> arr = [[1, 2], [3, 4]]
866 >>> mask = [[0, 1], [0, 0]]
867 >>> x = np.ma.array(arr, mask=mask)
868 >>> np.ma.compress_nd(x, axis=0)
869 array([[3, 4]])
870 >>> np.ma.compress_nd(x, axis=1)
871 array([[1],
872 [3]])
873 >>> np.ma.compress_nd(x)
874 array([[3]])
875
876 """
877 x = asarray(x)
878 m = getmask(x)
879 # Set axis to tuple of ints
880 if axis is None:
881 axis = tuple(range(x.ndim))
882 else:
883 axis = normalize_axis_tuple(axis, x.ndim)
884
885 # Nothing is masked: return x
886 if m is nomask or not m.any():
887 return x._data
888 # All is masked: return empty
889 if m.all():
890 return nxarray([])
891 # Filter elements through boolean indexing
892 data = x._data
893 for ax in axis:
894 axes = tuple(list(range(ax)) + list(range(ax + 1, x.ndim)))
895 data = data[(slice(None),) * ax + (~m.any(axis=axes),)]
896 return data
897
898

Callers 2

test_compress_ndMethod · 0.90
compress_rowcolsFunction · 0.85

Calls 6

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

Tested by 1

test_compress_ndMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…