Skip to content

unstack

Splits an array into a sequence of arrays along the given axis.

Parameters:

Name Type Description Default
x SparseArray

Input sparse arrays.

required
axis int

Axis along which the array will be split

0

Returns:

Name Type Description
out Tuple[SparseArray, ...]

Tuple of slices along the given dimension. All the arrays have the same shape.

Source code in sparse/numba_backend/_common.py
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
def unstack(x, axis=0):
    """
    Splits an array into a sequence of arrays along the given axis.

    Parameters
    ----------
    x : SparseArray
        Input sparse arrays.
    axis : int
        Axis along which the array will be split

    Returns
    -------
    out : Tuple[SparseArray,...]
        Tuple of slices along the given dimension. All the arrays have the same shape.
    """
    ndim = x.ndim

    if not (-ndim <= axis < ndim):
        raise ValueError(f"axis must be in range [-{ndim}, {ndim}), got {axis}")

    if not isinstance(x, SparseArray):
        raise TypeError("`a` must be a SparseArray.")

    if axis < 0:
        axis = ndim + axis
    new_order = (axis,) + tuple(i for i in range(ndim) if i != axis)
    x = x.transpose(new_order)
    return (*x,)