跳到内容

ones

返回一个给定形状和类型、并填充全一值的SparseArray。

参数

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

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

必需
dtype 数据 - 类型

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

float
format str

格式字符串。

'coo'
compressed_axes iterable

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

必需

返回值

名称 类型 描述
out SparseArray

具有给定形状和数据类型的全一数组。

示例

>>> ones(5).todense()
array([1., 1., 1., 1., 1.])
>>> ones((2, 2), dtype=int).todense()
array([[1, 1],
       [1, 1]])
源代码位于 sparse/numba_backend/_common.py
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
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)