Helper to support the order kwarg only for NumPy-backed arrays Memory layout parameter `order` is not exposed in the Array API standard, however some input validation code in scikit-learn needs to work both for classes and functions that will leverage Array API only operations and f
(
array, dtype=None, order=None, copy=None, *, xp=None, device=None
)
| 953 | |
| 954 | |
| 955 | def _asarray_with_order( |
| 956 | array, dtype=None, order=None, copy=None, *, xp=None, device=None |
| 957 | ): |
| 958 | """Helper to support the order kwarg only for NumPy-backed arrays |
| 959 | |
| 960 | Memory layout parameter `order` is not exposed in the Array API standard, |
| 961 | however some input validation code in scikit-learn needs to work both |
| 962 | for classes and functions that will leverage Array API only operations |
| 963 | and for code that inherently relies on NumPy backed data containers with |
| 964 | specific memory layout constraints (e.g. our own Cython code). The |
| 965 | purpose of this helper is to make it possible to share code for data |
| 966 | container validation without memory copies for both downstream use cases: |
| 967 | the `order` parameter is only enforced if the input array implementation |
| 968 | is NumPy based, otherwise `order` is just silently ignored. |
| 969 | """ |
| 970 | xp, _ = get_namespace(array, xp=xp) |
| 971 | if _is_numpy_namespace(xp): |
| 972 | # Use NumPy API to support order |
| 973 | if copy is True: |
| 974 | array = numpy.array(array, order=order, dtype=dtype) |
| 975 | else: |
| 976 | array = numpy.asarray(array, order=order, dtype=dtype) |
| 977 | |
| 978 | # At this point array is a NumPy ndarray. We convert it to an array |
| 979 | # container that is consistent with the input's namespace. |
| 980 | return xp.asarray(array) |
| 981 | else: |
| 982 | return xp.asarray(array, dtype=dtype, copy=copy, device=device) |
| 983 | |
| 984 | |
| 985 | def _ravel(array, xp=None): |
searching dependent graphs…