Test whether each element of an array is also present in a second array. The output is always a masked array. See `numpy.in1d` for more details. We recommend using :func:`isin` instead of `in1d` for new code. See Also -------- isin : Version of this function tha
(ar1, ar2, assume_unique=False, invert=False)
| 1287 | |
| 1288 | |
| 1289 | def in1d(ar1, ar2, assume_unique=False, invert=False): |
| 1290 | """ |
| 1291 | Test whether each element of an array is also present in a second |
| 1292 | array. |
| 1293 | |
| 1294 | The output is always a masked array. See `numpy.in1d` for more details. |
| 1295 | |
| 1296 | We recommend using :func:`isin` instead of `in1d` for new code. |
| 1297 | |
| 1298 | See Also |
| 1299 | -------- |
| 1300 | isin : Version of this function that preserves the shape of ar1. |
| 1301 | numpy.in1d : Equivalent function for ndarrays. |
| 1302 | |
| 1303 | Notes |
| 1304 | ----- |
| 1305 | .. versionadded:: 1.4.0 |
| 1306 | |
| 1307 | """ |
| 1308 | if not assume_unique: |
| 1309 | ar1, rev_idx = unique(ar1, return_inverse=True) |
| 1310 | ar2 = unique(ar2) |
| 1311 | |
| 1312 | ar = ma.concatenate((ar1, ar2)) |
| 1313 | # We need this to be a stable sort, so always use 'mergesort' |
| 1314 | # here. The values from the first array should always come before |
| 1315 | # the values from the second array. |
| 1316 | order = ar.argsort(kind='mergesort') |
| 1317 | sar = ar[order] |
| 1318 | if invert: |
| 1319 | bool_ar = (sar[1:] != sar[:-1]) |
| 1320 | else: |
| 1321 | bool_ar = (sar[1:] == sar[:-1]) |
| 1322 | flag = ma.concatenate((bool_ar, [invert])) |
| 1323 | indx = order.argsort(kind='mergesort')[:len(ar1)] |
| 1324 | |
| 1325 | if assume_unique: |
| 1326 | return flag[indx] |
| 1327 | else: |
| 1328 | return flag[indx][rev_idx] |
| 1329 | |
| 1330 | |
| 1331 | def isin(element, test_elements, assume_unique=False, invert=False): |