跳到内容

ones_like

返回一个与a具有相同形状和类型的全1 SparseArray。

参数

名称 类型 描述 默认值
a 类数组

结果的形状和数据类型将与 a 的形状和数据类型匹配。

必需
dtype 数据 - 类型

覆盖结果的数据类型。

None
format str

格式字符串。

None
compressed_axes 可迭代对象

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

必需

返回值

名称 类型 描述
out SparseArray

a具有相同形状和类型的全1数组。

示例

>>> x = np.ones((2, 3), dtype="i8")
>>> ones_like(x).todense()
array([[1, 1, 1],
       [1, 1, 1]])
源代码位于 sparse/numba_backend/_common.py
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
def ones_like(a, dtype=None, shape=None, format=None, *, device=None, **kwargs):
    """Return a SparseArray of ones with the same shape and type as ``a``.

    Parameters
    ----------
    a : array_like
        The shape and data-type of the result will match those of `a`.
    dtype : data-type, optional
        Overrides the data type of the result.
    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 same shape and type as `a`.

    Examples
    --------
    >>> x = np.ones((2, 3), dtype="i8")
    >>> ones_like(x).todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[1, 1, 1],
           [1, 1, 1]])
    """
    return full_like(a, fill_value=1, dtype=dtype, shape=shape, format=format, device=device, **kwargs)