Implementation:Scikit learn Scikit learn ArrayApiDelegation
| Knowledge Sources | |
|---|---|
| Domains | Utilities, Array API |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for delegating array operations to backend-specific implementations via the Array API standard provided by scikit-learn.
Description
This module provides delegation functions that route array operations to the appropriate backend-specific implementations based on the input array's namespace (NumPy, CuPy, Dask, JAX, PyTorch, or PyData Sparse). It implements Public API functions like isclose, nan_to_num, one_hot, and pad that work transparently across different array backends. The module detects which array library is being used and dispatches to optimized implementations accordingly.
Usage
Use this module when writing scikit-learn code that needs to work with multiple array backends (NumPy, CuPy, JAX, etc.) through the Python Array API standard, ensuring consistent behavior across backends.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/externals/array_api_extra/_delegation.py
Signature
def isclose(
a: Array | complex,
b: Array | complex,
*,
rtol: float = 1e-05,
atol: float = 1e-08,
equal_nan: bool = False,
xp: ModuleType | None = None,
) -> Array
def nan_to_num(...)
def one_hot(...)
def pad(...)
Import
from sklearn.externals.array_api_extra._delegation import isclose, nan_to_num, one_hot, pad
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| a | Array or complex | Yes | First input array or scalar for comparison |
| b | Array or complex | Yes | Second input array or scalar for comparison |
| rtol | float | No | Relative tolerance parameter (default: 1e-05) |
| atol | float | No | Absolute tolerance parameter (default: 1e-08) |
| equal_nan | bool | No | Whether to treat NaN values as equal (default: False) |
| xp | ModuleType or None | No | Array namespace to use; inferred if not provided |
Outputs
| Name | Type | Description |
|---|---|---|
| result | Array | Boolean array where elements are True where inputs are close |
Usage Examples
Basic Usage
import numpy as np
from sklearn.externals.array_api_extra._delegation import isclose
a = np.array([1.0, 2.0, 3.0])
b = np.array([1.0, 2.00001, 3.1])
result = isclose(a, b, atol=1e-4)
print(result) # [ True True False]