Return the elements of an array that satisfy some condition. This is equivalent to ``np.compress(ravel(condition), ravel(arr))``. If `condition` is boolean ``np.extract`` is equivalent to ``arr[condition]``. Note that `place` does the exact opposite of `extract`. Parameters
(condition, arr)
| 2053 | |
| 2054 | @array_function_dispatch(_extract_dispatcher) |
| 2055 | def extract(condition, arr): |
| 2056 | """ |
| 2057 | Return the elements of an array that satisfy some condition. |
| 2058 | |
| 2059 | This is equivalent to ``np.compress(ravel(condition), ravel(arr))``. If |
| 2060 | `condition` is boolean ``np.extract`` is equivalent to ``arr[condition]``. |
| 2061 | |
| 2062 | Note that `place` does the exact opposite of `extract`. |
| 2063 | |
| 2064 | Parameters |
| 2065 | ---------- |
| 2066 | condition : array_like |
| 2067 | An array whose nonzero or True entries indicate the elements of `arr` |
| 2068 | to extract. |
| 2069 | arr : array_like |
| 2070 | Input array of the same size as `condition`. |
| 2071 | |
| 2072 | Returns |
| 2073 | ------- |
| 2074 | extract : ndarray |
| 2075 | Rank 1 array of values from `arr` where `condition` is True. |
| 2076 | |
| 2077 | See Also |
| 2078 | -------- |
| 2079 | take, put, copyto, compress, place |
| 2080 | |
| 2081 | Examples |
| 2082 | -------- |
| 2083 | >>> import numpy as np |
| 2084 | >>> arr = np.arange(12).reshape((3, 4)) |
| 2085 | >>> arr |
| 2086 | array([[ 0, 1, 2, 3], |
| 2087 | [ 4, 5, 6, 7], |
| 2088 | [ 8, 9, 10, 11]]) |
| 2089 | >>> condition = np.mod(arr, 3)==0 |
| 2090 | >>> condition |
| 2091 | array([[ True, False, False, True], |
| 2092 | [False, False, True, False], |
| 2093 | [False, True, False, False]]) |
| 2094 | >>> np.extract(condition, arr) |
| 2095 | array([0, 3, 6, 9]) |
| 2096 | |
| 2097 | |
| 2098 | If `condition` is boolean: |
| 2099 | |
| 2100 | >>> arr[condition] |
| 2101 | array([0, 3, 6, 9]) |
| 2102 | |
| 2103 | """ |
| 2104 | return _nx.take(ravel(arr), nonzero(ravel(condition))[0]) |
| 2105 | |
| 2106 | |
| 2107 | def _place_dispatcher(arr, mask, vals): |
searching dependent graphs…