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)
| 1860 | |
| 1861 | @array_function_dispatch(_extract_dispatcher) |
| 1862 | def extract(condition, arr): |
| 1863 | """ |
| 1864 | Return the elements of an array that satisfy some condition. |
| 1865 | |
| 1866 | This is equivalent to ``np.compress(ravel(condition), ravel(arr))``. If |
| 1867 | `condition` is boolean ``np.extract`` is equivalent to ``arr[condition]``. |
| 1868 | |
| 1869 | Note that `place` does the exact opposite of `extract`. |
| 1870 | |
| 1871 | Parameters |
| 1872 | ---------- |
| 1873 | condition : array_like |
| 1874 | An array whose nonzero or True entries indicate the elements of `arr` |
| 1875 | to extract. |
| 1876 | arr : array_like |
| 1877 | Input array of the same size as `condition`. |
| 1878 | |
| 1879 | Returns |
| 1880 | ------- |
| 1881 | extract : ndarray |
| 1882 | Rank 1 array of values from `arr` where `condition` is True. |
| 1883 | |
| 1884 | See Also |
| 1885 | -------- |
| 1886 | take, put, copyto, compress, place |
| 1887 | |
| 1888 | Examples |
| 1889 | -------- |
| 1890 | >>> arr = np.arange(12).reshape((3, 4)) |
| 1891 | >>> arr |
| 1892 | array([[ 0, 1, 2, 3], |
| 1893 | [ 4, 5, 6, 7], |
| 1894 | [ 8, 9, 10, 11]]) |
| 1895 | >>> condition = np.mod(arr, 3)==0 |
| 1896 | >>> condition |
| 1897 | array([[ True, False, False, True], |
| 1898 | [False, False, True, False], |
| 1899 | [False, True, False, False]]) |
| 1900 | >>> np.extract(condition, arr) |
| 1901 | array([0, 3, 6, 9]) |
| 1902 | |
| 1903 | |
| 1904 | If `condition` is boolean: |
| 1905 | |
| 1906 | >>> arr[condition] |
| 1907 | array([0, 3, 6, 9]) |
| 1908 | |
| 1909 | """ |
| 1910 | return _nx.take(ravel(arr), nonzero(ravel(condition))[0]) |
| 1911 | |
| 1912 | |
| 1913 | def _place_dispatcher(arr, mask, vals): |