(condition, a, axis=None)
| 2015 | |
| 2016 | @derived_from(np) |
| 2017 | def compress(condition, a, axis=None): |
| 2018 | if not is_arraylike(condition): |
| 2019 | # Allow `condition` to be anything array-like, otherwise ensure `condition` |
| 2020 | # is a numpy array. |
| 2021 | condition = np.asarray(condition) |
| 2022 | condition = condition.astype(bool) |
| 2023 | a = asarray(a) |
| 2024 | |
| 2025 | if condition.ndim != 1: |
| 2026 | raise ValueError("Condition must be one dimensional") |
| 2027 | |
| 2028 | if axis is None: |
| 2029 | a = a.ravel() |
| 2030 | axis = 0 |
| 2031 | axis = validate_axis(axis, a.ndim) |
| 2032 | |
| 2033 | # Treat `condition` as filled with `False` (if it is too short) |
| 2034 | a = a[ |
| 2035 | tuple( |
| 2036 | slice(None, len(condition)) if i == axis else slice(None) |
| 2037 | for i in range(a.ndim) |
| 2038 | ) |
| 2039 | ] |
| 2040 | |
| 2041 | # Use `condition` to select along 1 dimension |
| 2042 | a = a[tuple(condition if i == axis else slice(None) for i in range(a.ndim))] |
| 2043 | |
| 2044 | return a |
| 2045 | |
| 2046 | |
| 2047 | @derived_from(np) |
searching dependent graphs…