2

Compare arrays of two different shapes for example:

a:
([[1,2],
[3,4],
[5,6],
[7,8],
[9,10]])

b:
([[3,4],
[5,6]])

c = a -b

expected output:

c:
([[1,2],
[7,8],
[9,10]])

So far what I've tried usually results in: operands could not be broadcast together with shapes (21,2) (5,2)

  • 1
    Basically it look slike you want to compare these as "sets", and thus calculate the set difference? – Willem Van Onsem Jun 30 '19 at 12:48
  • Basically yes, you can say that. – Jawad Ahmed Jun 30 '19 at 12:53
  • Possible duplicate of [Find the set difference between two large arrays (matrices) in Python](https://stackoverflow.com/questions/11903083/find-the-set-difference-between-two-large-arrays-matrices-in-python) – Georgy Jun 30 '19 at 14:07

1 Answers1

2

numpy has a setdiff1d function, the problem is it ravels the arrays (so they become 1d) and the result is also 1d.

with a tiny trick using .view we can make numpy think your arrays are already 1d, with tuple elements, and then use setdiff1d and reshape it afterwards.

try this:

import numpy as np

a = np.array([[1, 2],
              [3, 4],
              [5, 6],
              [7, 8],
              [9, 10]])

b = np.array([[3, 4],
              [5, 6]])

a_rows = a.view([('', a.dtype)] * a.shape[1])
b_rows = b.view([('', b.dtype)] * b.shape[1])

c = np.setdiff1d(a_rows, b_rows).view(a.dtype).reshape(-1, a.shape[1])

print(c)

Output:

[[ 1  2]
 [ 7  8]
 [ 9 10]]
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38