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)
| 1950 | |
| 1951 | @array_function_dispatch(_trim_zeros) |
| 1952 | def 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 |
searching dependent graphs…