remove zero lines 2-D numpy array

Solution 1:

Use np.all with an axis argument:

>>> r[np.all(r == 0, axis=1)]
array([[ 0.,  0.,  0.]])
>>> r[~np.all(r == 0, axis=1)]
array([[-1.41421356, -0.70710678, -0.70710678],
       [ 0.        , -1.22474487, -1.22474487]])

Solution 2:

Because the data are not equal zero exactly, we need set a threshold value for zero such as 1e-6, use numpy.all with axis=1 to check the rows are zeros or not. Use numpy.where and numpy.diff to get the split positions, and call numpy.split to split the array into a list of arrays.

import numpy as np
[q,r] = np.linalg.qr(np.array([1,0,0,0,1,1,1,1,1]).reshape(3,3))
mask = np.all(np.abs(r) < 1e-6, axis=1)
pos = np.where(np.diff(mask))[0] + 1
result = np.split(r, pos)

Solution 3:

Since this is among the first google results to trim a 2D array of zero lines, I want to add my implementation to only remove leading and trailing zeros, in two dimensions:

p = np.where(t != 0)
t = t[min(p[0]) : max(p[0]) + 1, min(p[1]) : max(p[1]) + 1]

This assumes your array is called t and numpy is imported as np.