bundles / numpy 2.4.3 / numpy / trim_zeros
_ArrayFunctionDispatcher
numpy:trim_zeros
Signature
def trim_zeros ( filt , trim = fb , axis = None ) Summary
Remove values along a dimension which are zero along all other.
Parameters
filt: array_likeInput array.
trim: {"fb", "f", "b"}, optionalA string with 'f' representing trim from front and 'b' to trim from back. By default, zeros are trimmed on both sides. Front and back refer to the edges of a dimension, with "front" referring to the side with the lowest index 0, and "back" referring to the highest index (or index -1).
axis: int or sequence, optionalIf None,
filtis cropped such that the smallest bounding box is returned that still contains all values which are not zero. If an axis is specified,filtwill be sliced in that dimension only on the sides specified bytrim. The remaining area will be the smallest that still contains all values wich are not zero.
Returns
trimmed: ndarray or sequenceThe result of trimming the input. The number of dimensions and the input data type are preserved.
Notes
For all-zero arrays, the first axis is trimmed first.
Examples
import numpy as np a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0)) np.trim_zeros(a)✓
np.trim_zeros(a, trim='b')
✓b = np.array([[0, 0, 2, 3, 0, 0], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 0, 0]]) np.trim_zeros(b)✓
np.trim_zeros(b, axis=-1)
✓np.trim_zeros([0, 1, 2, 0])
✓Aliases
-
numpy.trim_zeros