跳到内容

返回一个具有给定形状和类型,并用 fill_value 填充的稀疏数组。

参数

名称 类型 描述 默认值
形状 整型或整数元组

新数组的形状,例如 (2, 3)2

必需
填充值 标量

填充值。

必需
数据类型 数据 - 类型

数组所需的数据类型。默认值 None 表示 np.array(fill_value).dtype

格式 字符串

格式字符串。

'coo'
压缩轴 可迭代对象

如果返回 GCXS 数组,要压缩的轴。

必需
顺序 (C, 无)

目前不支持除这些值之外的任何值,并将引发 NotImplementedError。

'C'

返回值

名称 类型 描述
输出 稀疏数组

具有给定形状和数据类型,并用 fill_value 填充的数组。

示例

>>> full(5, 9).todense()
array([9, 9, 9, 9, 9])
>>> full((2, 2), 9, dtype=float).todense()
array([[9., 9.],
       [9., 9.]])
源代码位于 sparse/numba_backend/_common.py
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
@_check_device
def full(shape, fill_value, dtype=None, format="coo", order="C", *, device=None, **kwargs):
    """Return a SparseArray of given shape and type, filled with `fill_value`.

    Parameters
    ----------
    shape : int or tuple of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    fill_value : scalar
        Fill value.
    dtype : data-type, optional
        The desired data-type for the array. The default, `None`, means
        `np.array(fill_value).dtype`.
    format : str, optional
        A format string.
    compressed_axes : iterable, optional
        The axes to compress if returning a GCXS array.
    order : {'C', None}
        Values except these are not currently supported and raise a
        NotImplementedError.

    Returns
    -------
    out : SparseArray
        Array of `fill_value` with the given shape and dtype.

    Examples
    --------
    >>> full(5, 9).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([9, 9, 9, 9, 9])

    >>> full((2, 2), 9, dtype=float).todense()  # doctest: +SKIP
    array([[9., 9.],
           [9., 9.]])
    """
    from sparse import COO

    if dtype is None:
        dtype = np.array(fill_value).dtype
    if not isinstance(shape, tuple):
        shape = (shape,)
    if order not in {"C", None}:
        raise NotImplementedError("Currently, only 'C' and None are supported.")
    data = np.empty(0, dtype=dtype)
    coords = np.empty((len(shape), 0), dtype=np.intp)
    return COO(
        coords,
        data=data,
        shape=shape,
        fill_value=fill_value,
        has_duplicates=False,
        sorted=True,
    ).asformat(format, **kwargs)