Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: how delete rows common to 2 matrices

problem is very simple: I have two 2d np.array and I want to get a third array that only contains the rows that are not in common with the latter twos.

for example:

X = np.array([[0,1],[1,2],[4,5],[5,6],[8,9],[9,10]])
Y = np.array([[5,6],[9,10]])

Z = function(X,Y)
Z = array([[0, 1],
          [1, 2],
          [4, 5],
          [8, 9]])

I tried np.delete(X,Y,axis=0) but it doesn't work...

like image 555
rugrag Avatar asked Apr 23 '17 13:04

rugrag


People also ask

How do I delete rows in NumPy array based on condition?

np. delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on given index conditions and axis specified, the parameter ndarray is the array on which the manipulation will happen, the index is the particular rows based on conditions to be deleted, axis=0 for removing rows in our case.


1 Answers

Z = np.vstack(row for row in X if row not in Y)
like image 184
Luchko Avatar answered Sep 23 '22 00:09

Luchko