array-api-compat is a small wrapper around popular array libraries like NumPy, CuPy, PyTorch, Dask, JAX, ndonnx, and Sparse. Its primary goal is to make these libraries compatible with the Python Array API standard, allowing array-consuming libraries to write array-agnostic code today, even if the underlying array libraries do not yet fully implement the standard. The library is actively maintained by the Consortium for Python Data API Standards. The current version is 1.14.0, with ongoing development.
pip install array-api-compatVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the recommended usage of `array-api-compat` by obtaining an Array API compliant namespace using `array_namespace()`. This namespace (`xp`) can then be used with any supported array type (like NumPy, PyTorch, CuPy, etc.), enabling generic array-agnostic code.
Upgrade to Python 3.9 or newer.
Rely on functions from the `xp` (array_api_compat) namespace rather than direct array object methods/operators for Array API compliant behavior. Use helper functions like `array_api_compat.size()` where native library methods are incompatible (e.g., `torch.Tensor.size`).
Consult the `array-api-compat` changelog and test thoroughly when upgrading NumPy 2.x or `array-api-compat` versions to ensure expected behavior.
Be aware that the `xp` namespace will only contain functions specified by the Array API standard. For non-standard functions, you may need to use library-specific imports or `array-api-extra`.
Exercise caution when using sorting, linear algebra, or FFT operations with Dask arrays via `array-api-compat`. Verify behavior against Dask's native implementation if unexpected results occur.
Install the package using pip: 'pip install array-api-compat'.
Ensure you have the latest version of 'array-api-compat' installed: 'pip install --upgrade array-api-compat'.
Use the correct import statement: 'import array_api_compat.numpy as np'.
Pass at least one array object to 'array_namespace()' to determine the appropriate namespace.
Ensure that the array passed is from a supported library like NumPy, CuPy, PyTorch, Dask, JAX, ndonnx, or Sparse.