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.atleast2d(a).T`` ac
(a, axes=None)
| 587 | |
| 588 | @array_function_dispatch(_transpose_dispatcher) |
| 589 | def transpose(a, axes=None): |
| 590 | """ |
| 591 | Returns an array with axes transposed. |
| 592 | |
| 593 | For a 1-D array, this returns an unchanged view of the original array, as a |
| 594 | transposed vector is simply the same vector. |
| 595 | To convert a 1-D array into a 2-D column vector, an additional dimension |
| 596 | must be added, e.g., ``np.atleast2d(a).T`` achieves this, as does |
| 597 | ``a[:, np.newaxis]``. |
| 598 | For a 2-D array, this is the standard matrix transpose. |
| 599 | For an n-D array, if axes are given, their order indicates how the |
| 600 | axes are permuted (see Examples). If axes are not provided, then |
| 601 | ``transpose(a).shape == a.shape[::-1]``. |
| 602 | |
| 603 | Parameters |
| 604 | ---------- |
| 605 | a : array_like |
| 606 | Input array. |
| 607 | axes : tuple or list of ints, optional |
| 608 | If specified, it must be a tuple or list which contains a permutation |
| 609 | of [0,1,...,N-1] where N is the number of axes of `a`. The `i`'th axis |
| 610 | of the returned array will correspond to the axis numbered ``axes[i]`` |
| 611 | of the input. If not specified, defaults to ``range(a.ndim)[::-1]``, |
| 612 | which reverses the order of the axes. |
| 613 | |
| 614 | Returns |
| 615 | ------- |
| 616 | p : ndarray |
| 617 | `a` with its axes permuted. A view is returned whenever possible. |
| 618 | |
| 619 | See Also |
| 620 | -------- |
| 621 | ndarray.transpose : Equivalent method. |
| 622 | moveaxis : Move axes of an array to new positions. |
| 623 | argsort : Return the indices that would sort an array. |
| 624 | |
| 625 | Notes |
| 626 | ----- |
| 627 | Use ``transpose(a, argsort(axes))`` to invert the transposition of tensors |
| 628 | when using the `axes` keyword argument. |
| 629 | |
| 630 | Examples |
| 631 | -------- |
| 632 | >>> a = np.array([[1, 2], [3, 4]]) |
| 633 | >>> a |
| 634 | array([[1, 2], |
| 635 | [3, 4]]) |
| 636 | >>> np.transpose(a) |
| 637 | array([[1, 3], |
| 638 | [2, 4]]) |
| 639 | |
| 640 | >>> a = np.array([1, 2, 3, 4]) |
| 641 | >>> a |
| 642 | array([1, 2, 3, 4]) |
| 643 | >>> np.transpose(a) |
| 644 | array([1, 2, 3, 4]) |
| 645 | |
| 646 | >>> a = np.ones((1, 2, 3)) |
no test coverage detected