flopscope.numpy.trim_zeros
fnp.trim_zeros(filt, trim='fb', axis=None)[flopscope source][numpy source]
Remove values along a dimension which are zero along all other.
Adapted from NumPy docs np.trim_zeros
Trim leading/trailing zeros from 1-D array. Cost: num trimmed.
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 both sides. Front and back refer to the edges of a dimension, with "front" refering to the side with the lowest index 0, and "back" refering to the highest index (or index -1).
- axis:int or sequence, optional
If 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 sequence
The 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 flopscope.numpy as fnp
>>> a = flops.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
>>> flops.trim_zeros(a)
array([1, 2, 3, 0, 2, 1])>>> flops.trim_zeros(a, trim='b')
array([0, 0, 0, ..., 0, 2, 1])Multiple dimensions are supported.
>>> b = flops.array([[0, 0, 2, 3, 0, 0],
... [0, 1, 0, 3, 0, 0],
... [0, 0, 0, 0, 0, 0]])
>>> flops.trim_zeros(b)
array([[0, 2, 3],
[1, 0, 3]])>>> flops.trim_zeros(b, axis=-1)
array([[0, 2, 3],
[1, 0, 3],
[0, 0, 0]])The input data type is preserved, list/tuple in means list/tuple out.
>>> flops.trim_zeros([0, 1, 2, 0])
[1, 2]