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

Function trim_zeros

numpy/lib/_function_base_impl.py:1952–2047  ·  view source on GitHub ↗

Remove values along a dimension which are zero along all other. Parameters ---------- filt : array_like Input array. trim : {"fb", "f", "b"}, optional A string with 'f' representing trim from front and 'b' to trim from back. By default, zeros are trimmed on b

(filt, trim='fb', axis=None)

Source from the content-addressed store, hash-verified

1950
1951@array_function_dispatch(_trim_zeros)
1952def trim_zeros(filt, trim='fb', axis=None):
1953 """Remove values along a dimension which are zero along all other.
1954
1955 Parameters
1956 ----------
1957 filt : array_like
1958 Input array.
1959 trim : {"fb", "f", "b"}, optional
1960 A string with 'f' representing trim from front and 'b' to trim from
1961 back. By default, zeros are trimmed on both sides.
1962 Front and back refer to the edges of a dimension, with "front" referring
1963 to the side with the lowest index 0, and "back" referring to the highest
1964 index (or index -1).
1965 axis : int or sequence, optional
1966 If None, `filt` is cropped such that the smallest bounding box is
1967 returned that still contains all values which are not zero.
1968 If an axis is specified, `filt` will be sliced in that dimension only
1969 on the sides specified by `trim`. The remaining area will be the
1970 smallest that still contains all values which are not zero.
1971
1972 .. versionadded:: 2.2.0
1973
1974 Returns
1975 -------
1976 trimmed : ndarray or sequence
1977 The result of trimming the input. The number of dimensions and the
1978 input data type are preserved.
1979
1980 Notes
1981 -----
1982 For all-zero arrays, the first axis is trimmed first.
1983
1984 Examples
1985 --------
1986 >>> import numpy as np
1987 >>> a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
1988 >>> np.trim_zeros(a)
1989 array([1, 2, 3, 0, 2, 1])
1990
1991 >>> np.trim_zeros(a, trim='b')
1992 array([0, 0, 0, ..., 0, 2, 1])
1993
1994 Multiple dimensions are supported.
1995
1996 >>> b = np.array([[0, 0, 2, 3, 0, 0],
1997 ... [0, 1, 0, 3, 0, 0],
1998 ... [0, 0, 0, 0, 0, 0]])
1999 >>> np.trim_zeros(b)
2000 array([[0, 2, 3],
2001 [1, 0, 3]])
2002
2003 >>> np.trim_zeros(b, axis=-1)
2004 array([[0, 2, 3],
2005 [1, 0, 3],
2006 [0, 0, 0]])
2007
2008 The input data type is preserved, list/tuple in means list/tuple out.
2009

Callers 14

__init__Method · 0.90
test_basicMethod · 0.90
test_leading_skipMethod · 0.90
test_trailing_skipMethod · 0.90
test_all_zeroMethod · 0.90
test_size_zeroMethod · 0.90
test_overflowMethod · 0.90
test_no_trimMethod · 0.90
test_list_to_listMethod · 0.90
test_nd_basicMethod · 0.90
test_allzeroMethod · 0.90
test_trim_argMethod · 0.90

Calls 3

_arg_trim_zerosFunction · 0.85
sliceFunction · 0.85
lowerMethod · 0.80

Tested by 13

test_basicMethod · 0.72
test_leading_skipMethod · 0.72
test_trailing_skipMethod · 0.72
test_all_zeroMethod · 0.72
test_size_zeroMethod · 0.72
test_overflowMethod · 0.72
test_no_trimMethod · 0.72
test_list_to_listMethod · 0.72
test_nd_basicMethod · 0.72
test_allzeroMethod · 0.72
test_trim_argMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…