跳到内容

返回一个给定形状和类型的SparseArray,其中填充了零。

参数

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

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

必需
数据类型 数据 - 类型

数组所需的数据类型,例如 numpy.int8。默认为 numpy.float64

浮点型
格式 字符串

格式字符串。

'coo'
压缩轴 可迭代对象

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

必需

返回值

名称 类型 描述
输出 SparseArray

给定形状和数据类型的零数组。

示例

>>> zeros(5).todense()
array([0., 0., 0., 0., 0.])
>>> zeros((2, 2), dtype=int).todense()
array([[0, 0],
       [0, 0]])
源代码位于 sparse/numba_backend/_common.py
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
def zeros(shape, dtype=float, format="coo", *, device=None, **kwargs):
    """Return a SparseArray of given shape and type, filled with zeros.

    Parameters
    ----------
    shape : int or tuple of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    format : str, optional
        A format string.
    compressed_axes : iterable, optional
        The axes to compress if returning a GCXS array.

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

    Examples
    --------
    >>> zeros(5).todense()  # doctest: +SKIP
    array([0., 0., 0., 0., 0.])

    >>> zeros((2, 2), dtype=int).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[0, 0],
           [0, 0]])
    """
    return full(shape, fill_value=0, dtype=np.dtype(dtype), format=format, device=device, **kwargs)