Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove one value from a NumPy array

I am trying to all rows that only contain zeros from a NumPy array. For example, I want to remove [0,0] from

n = np.array([[1,2], [0,0], [5,6]])

and be left with:

np.array([[1,2], [5,6]])
like image 505
user1220022 Avatar asked Apr 12 '12 08:04

user1220022


2 Answers

To remove the second row from a numpy table:

import numpy
n = numpy.array([[1,2],[0,0],[5,6]])
new_n = numpy.delete(n, 1, axis=0)

To remove rows containing only 0:

import numpy
n = numpy.array([[1,2],[0,0],[5,6]])
idxs = numpy.any(n != 0, axis=1) # index of rows with at least one non zero value
n_non_zero = n[idxs, :] # selection of the wanted rows
like image 185
Simon Bergot Avatar answered Oct 07 '22 14:10

Simon Bergot


If you want to delete any row that only contains zeros, the fastest way I can think of is:

n = numpy.array([[1,2], [0,0], [5,6]])

keep_row = n.any(axis=1)  # Index of rows with at least one non-zero value
n_non_zero = n[keep_row]  # Rows to keep, only

This runs much faster than Simon's answer, because n.any() stops checking the values of each row as soon as it encounters any non-zero value (in Simon's answer, all the elements of each row are compared to zero first, which results in unnecessary computations).


Here is a generalization of the answer, if you ever need to remove a rows that have a specific value (instead of removing only rows that only contain zeros):

n = numpy.array([[1,2], [0,0], [5,6]])

to_be_removed = [0, 0]  # Can be any row values: [5, 6], etc.
other_rows = (n != to_be_removed).any(axis=1)  # Rows that have at least one element that differs
n_other_rows = n[other_rows]  # New array with rows equal to to_be_removed removed.

Note that this solution is not fully optimized: even if the first element of to_be_removed does not match, the remaining row elements from n are compared to those of to_be_removed (as in Simon's answer).

I'd be curious to know if there is a simple efficient NumPy solution to the more general problem of deleting rows with a specific value.

Using cython loops might be a fast solution: for each row, element comparison could be stopped as soon as one element from the row differs from the corresponding element in to_be_removed.

like image 33
Eric O Lebigot Avatar answered Oct 07 '22 15:10

Eric O Lebigot