跳到内容

asCOO

将输入转换为 sparse.COO。将 sparse.COO 对象原样传递。

参数

名称 类型 描述 默认值
x Union[SparseArray, spmatrix, ndarray]

要转换的输入数组。

必需
name str

用于异常中的操作名称。

'asCOO'
check bool_

是否检查密集输入。

True

返回值

类型 描述
COO

转换后的 sparse.COO 数组。

引发

类型 描述
ValueError

如果 check 为真且提供了密集输入。

源代码位于 sparse/numba_backend/_coo/common.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def asCOO(x, name="asCOO", check=True):
    """
    Convert the input to [`sparse.COO`][]. Passes through [`sparse.COO`][] objects as-is.

    Parameters
    ----------
    x : Union[SparseArray, scipy.sparse.spmatrix, numpy.ndarray]
        The input array to convert.
    name : str, optional
        The name of the operation to use in the exception.
    check : bool, optional
        Whether to check for a dense input.

    Returns
    -------
    COO
        The converted [`sparse.COO`][] array.

    Raises
    ------
    ValueError
        If `check` is true and a dense input is supplied.
    """
    from .._common import _is_sparse
    from .core import COO

    if check and not _is_sparse(x):
        raise ValueError(f"Performing this operation would produce a dense result: {name}")

    if not isinstance(x, COO):
        x = COO(x)

    return x