python - test for membership in a 2d numpy array -
i have 2 2d arrays of same size
a = array([[1,2],[3,4],[5,6]]) b = array([[1,2],[3,4],[7,8]])
i want know rows of b in a.
so output should :
array([ true, true, false], dtype=bool)
without making :
array([any(i == a) in b])
cause , b huge.
there function 1d arrays : in1d
what we'd use np.in1d
... except np.in1d
works 1-dimensional arrays. our arrays multi-dimensional. however, can view arrays 1-dimensional array of strings:
a = a.ravel().view((np.str, a.itemsize*a.shape[1]))
for example,
in [15]: = np.array([[1, 2], [2, 3], [1, 3]]) in [16]: = a.ravel().view((np.str, a.itemsize*a.shape[1])) in [17]: a.dtype out[17]: dtype('|s8') in [18]: a.shape out[18]: (3,) in [19]: out[19]: array(['\x01\x00\x00\x00\x02', '\x02\x00\x00\x00\x03', '\x01\x00\x00\x00\x03'], dtype='|s8')
this makes each row of a
string. matter of hooking np.in1d
:
def innd(a, b, assume_unique=false): = np.asarray(a, order='c') b = np.asarray(b, order='c') = a.ravel().view((np.str, a.itemsize * a.shape[1])) b = b.ravel().view((np.str, b.itemsize * b.shape[1])) return np.in1d(a, b, assume_unique)
import numpy np def innd(a, b, assume_unique=false): = np.asarray(a, order='c') b = np.asarray(b, order='c') = a.ravel().view((np.str, a.itemsize * a.shape[1])) b = b.ravel().view((np.str, b.itemsize * b.shape[1])) return np.in1d(a, b, assume_unique) tests = [ (np.array([[1, 2], [2, 3], [1, 3]]), np.array([[2, 2], [3, 3], [4, 4]]), np.array([false, false, false])), (np.array([[1, 2], [2, 2], [1, 3]]), np.array([[2, 2], [3, 3], [4, 4]]), np.array([true, false, false])), (np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 2], [3, 4], [7, 8]]), np.array([true, true, false])), (np.array([[1, 2], [5, 6], [3, 4]]), np.array([[1, 2], [5, 6], [7, 8]]), np.array([true, true, false])), (np.array([[-0.5, 2.5, -2, 100, 2], [5, 6, 7, 8, 9], [3, 4, 5, 6, 7]]), np.array([[1.0, 2, 3, 4, 5], [5, 6, 7, 8, 9], [-0.5, 2.5, -2, 100, 2]]), np.array([false, true, true])) ] a, b, answer in tests: result = innd(b, a) try: assert np.all(answer == result) except assertionerror: print('''\ a: {a} b: {b} answer: {answer} result: {result}'''.format(**locals())) raise else: print('success!')
yields
success!