Skip to content

zeros

Return a SparseArray of given shape and type, filled with zeros.

Parameters:

Name Type Description Default
shape int or tuple of ints

Shape of the new array, e.g., (2, 3) or 2.

required
dtype data - type

The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.

float
format str

A format string.

'coo'
compressed_axes iterable

The axes to compress if returning a GCXS array.

required

Returns:

Name Type Description
out SparseArray

Array of zeros with the given shape and dtype.

Examples:

>>> zeros(5).todense()
array([0., 0., 0., 0., 0.])
>>> zeros((2, 2), dtype=int).todense()
array([[0, 0],
       [0, 0]])
Source code in 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)