MCPcopy
hub / github.com/numpy/numpy / transpose

Function transpose

numpy/_core/fromnumeric.py:605–677  ·  view source on GitHub ↗

Returns an array with axes transposed. For a 1-D array, this returns an unchanged view of the original array, as a transposed vector is simply the same vector. To convert a 1-D array into a 2-D column vector, an additional dimension must be added, e.g., ``np.atleast_2d(a).T`` a

(a, axes=None)

Source from the content-addressed store, hash-verified

603
604@array_function_dispatch(_transpose_dispatcher)
605def transpose(a, axes=None):
606 """
607 Returns an array with axes transposed.
608
609 For a 1-D array, this returns an unchanged view of the original array, as a
610 transposed vector is simply the same vector.
611 To convert a 1-D array into a 2-D column vector, an additional dimension
612 must be added, e.g., ``np.atleast_2d(a).T`` achieves this, as does
613 ``a[:, np.newaxis]``.
614 For a 2-D array, this is the standard matrix transpose.
615 For an n-D array, if axes are given, their order indicates how the
616 axes are permuted (see Examples). If axes are not provided, then
617 ``transpose(a).shape == a.shape[::-1]``.
618
619 Parameters
620 ----------
621 a : array_like
622 Input array.
623 axes : tuple or list of ints, optional
624 If specified, it must be a tuple or list which contains a permutation
625 of [0, 1, ..., N-1] where N is the number of axes of `a`. Negative
626 indices can also be used to specify axes. The i-th axis of the returned
627 array will correspond to the axis numbered ``axes[i]`` of the input.
628 If not specified, defaults to ``range(a.ndim)[::-1]``, which reverses
629 the order of the axes.
630
631 Returns
632 -------
633 p : ndarray
634 `a` with its axes permuted. A view is returned whenever possible.
635
636 See Also
637 --------
638 ndarray.transpose : Equivalent method.
639 moveaxis : Move axes of an array to new positions.
640 argsort : Return the indices that would sort an array.
641
642 Notes
643 -----
644 Use ``transpose(a, argsort(axes))`` to invert the transposition of tensors
645 when using the `axes` keyword argument.
646
647 Examples
648 --------
649 >>> import numpy as np
650 >>> a = np.array([[1, 2], [3, 4]])
651 >>> a
652 array([[1, 2],
653 [3, 4]])
654 >>> np.transpose(a)
655 array([[1, 3],
656 [2, 4]])
657
658 >>> a = np.array([1, 2, 3, 4])
659 >>> a
660 array([1, 2, 3, 4])
661 >>> np.transpose(a)
662 array([1, 2, 3, 4])

Callers 5

rot90Function · 0.90
apply_along_axisFunction · 0.90
argwhereFunction · 0.70
moveaxisFunction · 0.70

Calls 1

_wrapfuncFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…