-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nschloe/isin
isin_rows
- Loading branch information
Showing
5 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import numpy as np | ||
|
||
|
||
def isin_rows(a, b): | ||
a = np.asarray(a) | ||
b = np.asarray(b) | ||
if not np.issubdtype(a.dtype, np.integer): | ||
raise ValueError(f"Input array must be integer type, got {a.dtype}.") | ||
if not np.issubdtype(b.dtype, np.integer): | ||
raise ValueError(f"Input array must be integer type, got {b.dtype}.") | ||
|
||
a = a.reshape(a.shape[0], np.prod(a.shape[1:], dtype=int)) | ||
b = b.reshape(b.shape[0], np.prod(b.shape[1:], dtype=int)) | ||
|
||
a_view = np.ascontiguousarray(a).view( | ||
np.dtype((np.void, a.dtype.itemsize * a.shape[1])) | ||
) | ||
b_view = np.ascontiguousarray(b).view( | ||
np.dtype((np.void, b.dtype.itemsize * b.shape[1])) | ||
) | ||
|
||
out = np.isin(a_view, b_view) | ||
|
||
return out.reshape(a.shape[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import numpy as np | ||
|
||
import npx | ||
|
||
|
||
def test_isin(): | ||
a = [[0, 3], [1, 0]] | ||
b = [[1, 0], [7, 12], [-1, 5]] | ||
|
||
out = npx.isin_rows(a, b) | ||
assert np.all(out == [False, True]) | ||
|
||
|
||
def test_scalar(): | ||
a = [0, 3, 5] | ||
b = [-1, 6, 5, 0, 0, 0] | ||
|
||
out = npx.isin_rows(a, b) | ||
assert np.all(out == [True, False, True]) |