I am trying to get a slice (for example elements 1-3 and 5-N) of an array A(N,3) avoiding using numpy.delete. And example of the process will be the following:
[[1,2,3],[4,5,6],[7,8,9],[3,2,1]] ==> [[1,2,3],[3,2,1]]
I was hoping to use something like
A[A != [1,2,3] ].reshape()
But that performs an element-wise comparison and thus removes more elements than I wanted to. How does one do it? I came up with this idea but seems too complex and slow:
A_removed = A[first_removed:last:removed,:]
mask = np.not_equal(A[:,None],A_removed)
mask = np.logical_and.reduce(mask,1)
A = A[mask].reshape()
Is there a way of doing it in a faster/cleaner way?
PD the asumption that any two elements of A can't be equal always holds
Rereading the question, I'm now pretty sure the OP wanted the inverse of what I originally posted. Here's how you get that:
import numpy as np
def selectRow(arr, selrow):
selset = set(selrow)
return np.array([row for row in arr if selset == set(row)])
arr = np.array([
[1,2,3],
[4,5,6],
[7,8,9],
[3,2,1]
])
selectRow(arr, [1,2,3])
Output:
array([[1, 2, 3],
[3, 2, 1]])
I'll leave the original answer up for the moment, just in case I'm wrong.
How about just:
import numpy as np
def withoutRow(arr, badrow):
return np.array([row for row in arr if not np.array_equal(row, badrow)])
which you would then use as so:
arr = np.array([
[1,2,3],
[4,5,6],
[7,8,9],
[3,2,1]
])
withoutRow(arr, [1,2,3])
Output:
array([[4, 5, 6],
[7, 8, 9],
[3, 2, 1]])
withoutRow should be fairly efficient (especially when compared to boolean indexing), since there's only a single loop (over the rows of the original array), and you only have to construct a single new array (the return value).
If you want to remove any point with matching coordinates without regard to the order of the coordinates, you could instead use:
def withoutRowUnordered(arr, badrow):
badset = set(badrow)
return np.array([row for row in arr if badset != set(row)])
withoutRowUnordered(arr, [1,2,3])
Output:
array([[4, 5, 6],
[7, 8, 9]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With