MCPcopy
hub / github.com/scikit-learn/scikit-learn / _asarray_with_order

Function _asarray_with_order

sklearn/utils/_array_api.py:955–982  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

953
954
955def _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
985def _ravel(array, xp=None):

Callers 4

check_arrayFunction · 0.90
column_or_1dFunction · 0.90
test_asarray_with_orderFunction · 0.90
_preprocess_dataFunction · 0.90

Calls 2

get_namespaceFunction · 0.85
_is_numpy_namespaceFunction · 0.85

Tested by 1

test_asarray_with_orderFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…