COO.transpose

COO.transpose(axes=None)[source]

Returns a new array which has the order of the axes switched.

Parameters:

axes (Iterable[int], optional) – The new order of the axes compared to the previous one. Reverses the axes by default.

Returns:

The new array with the axes in the desired order.

Return type:

COO

See also

COO.T

A quick property to reverse the order of the axes.

numpy.ndarray.transpose

Numpy equivalent function.

Examples

We can change the order of the dimensions of any COO array with this function.

>>> x = np.add.outer(np.arange(5), np.arange(5)[::-1])
>>> x  
array([[4, 3, 2, 1, 0],
       [5, 4, 3, 2, 1],
       [6, 5, 4, 3, 2],
       [7, 6, 5, 4, 3],
       [8, 7, 6, 5, 4]])
>>> s = COO.from_numpy(x)
>>> s.transpose((1, 0)).todense()  
array([[4, 5, 6, 7, 8],
       [3, 4, 5, 6, 7],
       [2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4]])

Note that by default, this reverses the order of the axes rather than switching the last and second-to-last axes as required by some linear algebra operations.

>>> x = np.random.rand(2, 3, 4)
>>> s = COO.from_numpy(x)
>>> s.transpose().shape
(4, 3, 2)