Skip to content

ones

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

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 ones with the given shape and dtype.

Examples:

>>> ones(5).todense()
array([1., 1., 1., 1., 1.])
>>> ones((2, 2), dtype=int).todense()
array([[1, 1],
       [1, 1]])
Source code in sparse/numba_backend/_common.py
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
def ones(shape, dtype=float, format="coo", *, device=None, **kwargs):
    """Return a SparseArray of given shape and type, filled with ones.

    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 ones with the given shape and dtype.

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

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