1

Given m x n nd array of floats, what is the best way to get an m' x n nd array of floats that does not contain all-zero rows?

for example: Given

[ 
  [1.0, 0.0, 2.0], 
  [0.0, 0.0, 0.0], 
  [2.0, 1.0, 0.0] 
]

I want to get

[ 
  [1.0, 0.0, 2.0], 
  [2.0, 1.0, 0.0] 
]
0

5 Answers 5

1

You can index using a boolean array:

a = np.array([[1.0, 0.0, 2.0], [0.0, 0.0, 0.0], [2.0, 1.0, 0.0]])

print(a[a.any(axis=1)])

Here a.any(axis=1) will be True where any elements in the row are non-zero. These are the rows that we want to keep.

Sign up to request clarification or add additional context in comments.

Comments

1

You can exclude those elements as follows:

>>> import numpy as np
>>> x = np.array([ [1.0, 0.0, 2.0], [0.0, 0.0, 0.0], [2.0, 1.0, 0.0] ])
>>> x
array([[1., 0., 2.],
       [0., 0., 0.],
       [2., 1., 0.]])
>>> sumrow = np.abs(x).sum(-1)
>>> x[sumrow>0]
array([[1., 0., 2.],
       [2., 1., 0.]])

Note: @Akavall pointed out correctly that np.abs() would prevent issues with negative values.

Additionally, another more complex approach:

>>> x = np.array([ [1.0, 0.0, 2.0], [0.0, 0.0, 0.0], [2.0, 1.0, 0.0] ])
>>> x[~np.all(x == 0, axis=1)]
array([[1., 0., 2.],
       [2., 1., 0.]])

See: https://www.geeksforgeeks.org/numpy-indexing/

4 Comments

What if a row contains non zero elements that sum to 0, for example: [-1., 0., 1.]?
I suppose np.abs(x).sum(-1) will solve the issue that I pointed out.
You're correct @Akavall - I just edited the answer to be correct. Also added another approach.
Instead of ~np.all(x == 0, axis=1) you can just do x.any(axis=1). This saves some computation steps and temporary arrays, although by De Morgan's Laws we can see that it arrives at the same answer.
0

A possible solution would be to use the fact that the sum of all zeros is zero. Create a mask using that fact:

>>> bar = np.array ([ [1.0, 0.0, 2.0], [0.0, 0.0, 0.0], [2.0, 1.0, 0.0] ] )
>>> mask = bar.sum(axis=1)==0
>>> bar[mask]
array([[1., 0., 2.],
       [2., 1., 0.]])

1 Comment

O, that's actually the same answer as @Damiox gave :)
0

Here's one way to do it:

import numpy as np
x    = np.array([ [1.0, 0.0, 2.0], [0.0, 0.0, 0.0], [2.0, 1.0, 0.0] ])
m, n = x.shape
rows = [row for row in range(m) if not all(x[row] == 0)]
x    = x[rows]
print(x)

This works for arrays containing negative data also. If we use sum suppose a row contains [-1, 0, 1] it will be deleted we don't want that.

Comments

0
a=np.array([r for r in a if any(r)])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.