跳到内容

permute_dims

置换数组 x 的轴(维度)。

参数

名称 类型 描述 默认值
x

输入数组。

必需
axes

包含 (0, 1, ..., N-1) 的排列的元组,其中 Nx 的轴(维度)数量。

返回值

名称 类型 描述
out 数组

包含轴置换的数组。返回的数组必须与 x 具有相同的数据类型。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.permute_dims(a, axes=(1, 0))
>>> o.todense()
array([[0, 2],
       [1, 0]])
源代码位于 sparse/numba_backend/_common.py
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
def permute_dims(x, /, axes=None):
    """
    Permutes the axes (dimensions) of an array ``x``.

    Parameters
    ----------
    x: array
        input array.
    axes: Tuple[int, ...]
        tuple containing a permutation of ``(0, 1, ..., N-1)`` where ``N`` is the number of axes (dimensions)
        of ``x``.

    Returns
    -------
    out: array
        an array containing the axes permutation. The returned array must have the same data type as ``x``.

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.permute_dims(a, axes=(1, 0))
    >>> o.todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[0, 2],
           [1, 0]])
    """

    return x.transpose(axes=axes)