Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use numpy to mask a row containing only zeros

I have a large array of point cloud data which is generated using the azure kinect. All erroneous measurements are assigned the coordinate [0,0,0]. I want to remove all coordinates with the value [0,0,0]. Since my array is rater large (1 million points) and since U need to do this process in real-time, speed is of the essence.

In my current approach I try to use numpy to mask out all rows that contain three zeroes ([0,0,0]). However, the np.ma.masked_equal function does not evaluate an entire row, but only evaluates single elements. As a result, rows that contain at least one 0 are already filtered by this approach. I only want rows to be filtered when all values in the row are 0. Find an example of my code below:

my_data = np.array([[1,2,3],[0,0,0],[3,4,5],[2,5,7],[0,0,1]])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my_data = np.ma.masked_equal(my_data, [0,0,0])

my_data = np.ma.compress_rows(my_data)

output

array([[1, 2, 3],
       [3, 4, 5],
       [2, 5, 7]])

desired output

array([[1, 2, 3],
       [3, 4, 5],
       [2, 5, 7],
       [0, 0, 1]])`

>Solution :

Find all data points that are 0 (doesn’t require np.ma module) and then select all rows that do not contain all zeros:

import numpy as np
my_data = np.array([[1, 2, 3], [0, 0, 0], [3, 4, 5], [2, 5, 7], [0, 0, 1]])

my_data[~(my_data == 0).all(axis= 1)]

Output:

array([[1, 2, 3],
       [3, 4, 5],
       [2, 5, 7],
       [0, 0, 1]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading