MCPcopy Index your code
hub / github.com/numpy/numpy / in1d

Function in1d

numpy/ma/extras.py:1387–1431  ·  view source on GitHub ↗

Test whether each element of an array is also present in a second array. The output is always a masked array. We recommend using :func:`isin` instead of `in1d` for new code. See Also -------- isin : Version of this function that preserves the shape of ar1.

(ar1, ar2, assume_unique=False, invert=False)

Source from the content-addressed store, hash-verified

1385
1386
1387def in1d(ar1, ar2, assume_unique=False, invert=False):
1388 """
1389 Test whether each element of an array is also present in a second
1390 array.
1391
1392 The output is always a masked array.
1393
1394 We recommend using :func:`isin` instead of `in1d` for new code.
1395
1396 See Also
1397 --------
1398 isin : Version of this function that preserves the shape of ar1.
1399
1400 Examples
1401 --------
1402 >>> import numpy as np
1403 >>> ar1 = np.ma.array([0, 1, 2, 5, 0])
1404 >>> ar2 = [0, 2]
1405 >>> np.ma.in1d(ar1, ar2)
1406 masked_array(data=[ True, False, True, False, True],
1407 mask=False,
1408 fill_value=True)
1409
1410 """
1411 if not assume_unique:
1412 ar1, rev_idx = unique(ar1, return_inverse=True)
1413 ar2 = unique(ar2)
1414
1415 ar = ma.concatenate((ar1, ar2))
1416 # We need this to be a stable sort, so always use 'mergesort'
1417 # here. The values from the first array should always come before
1418 # the values from the second array.
1419 order = ar.argsort(kind='mergesort')
1420 sar = ar[order]
1421 if invert:
1422 bool_ar = (sar[1:] != sar[:-1])
1423 else:
1424 bool_ar = (sar[1:] == sar[:-1])
1425 flag = ma.concatenate((bool_ar, [invert]))
1426 indx = order.argsort(kind='mergesort')[:len(ar1)]
1427
1428 if assume_unique:
1429 return flag[indx]
1430 else:
1431 return flag[indx][rev_idx]
1432
1433
1434def isin(element, test_elements, assume_unique=False, invert=False):

Callers 4

test_in1dMethod · 0.90
test_in1d_invertMethod · 0.90
isinFunction · 0.85
setdiff1dFunction · 0.85

Calls 2

uniqueFunction · 0.70
argsortMethod · 0.45

Tested by 2

test_in1dMethod · 0.72
test_in1d_invertMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…