Move axes of an array to new positions. Other axes remain in their original order. Parameters ---------- a : np.ndarray The array whose axes should be reordered. source : int or sequence of int Original positions of the axes to move. These must be unique.
(a, source, destination)
| 1487 | |
| 1488 | @array_function_dispatch(_moveaxis_dispatcher) |
| 1489 | def moveaxis(a, source, destination): |
| 1490 | """ |
| 1491 | Move axes of an array to new positions. |
| 1492 | |
| 1493 | Other axes remain in their original order. |
| 1494 | |
| 1495 | Parameters |
| 1496 | ---------- |
| 1497 | a : np.ndarray |
| 1498 | The array whose axes should be reordered. |
| 1499 | source : int or sequence of int |
| 1500 | Original positions of the axes to move. These must be unique. |
| 1501 | destination : int or sequence of int |
| 1502 | Destination positions for each of the original axes. These must also be |
| 1503 | unique. |
| 1504 | |
| 1505 | Returns |
| 1506 | ------- |
| 1507 | result : np.ndarray |
| 1508 | Array with moved axes. This array is a view of the input array. |
| 1509 | |
| 1510 | See Also |
| 1511 | -------- |
| 1512 | transpose : Permute the dimensions of an array. |
| 1513 | swapaxes : Interchange two axes of an array. |
| 1514 | |
| 1515 | Examples |
| 1516 | -------- |
| 1517 | >>> import numpy as np |
| 1518 | >>> x = np.zeros((3, 4, 5)) |
| 1519 | >>> np.moveaxis(x, 0, -1).shape |
| 1520 | (4, 5, 3) |
| 1521 | >>> np.moveaxis(x, -1, 0).shape |
| 1522 | (5, 3, 4) |
| 1523 | |
| 1524 | These all achieve the same result: |
| 1525 | |
| 1526 | >>> np.transpose(x).shape |
| 1527 | (5, 4, 3) |
| 1528 | >>> np.swapaxes(x, 0, -1).shape |
| 1529 | (5, 4, 3) |
| 1530 | >>> np.moveaxis(x, [0, 1], [-1, -2]).shape |
| 1531 | (5, 4, 3) |
| 1532 | >>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape |
| 1533 | (5, 4, 3) |
| 1534 | |
| 1535 | """ |
| 1536 | try: |
| 1537 | # allow duck-array types if they define transpose |
| 1538 | transpose = a.transpose |
| 1539 | except AttributeError: |
| 1540 | a = asarray(a) |
| 1541 | transpose = a.transpose |
| 1542 | |
| 1543 | source = normalize_axis_tuple(source, a.ndim, 'source') |
| 1544 | destination = normalize_axis_tuple(destination, a.ndim, 'destination') |
| 1545 | if len(source) != len(destination): |
| 1546 | raise ValueError('`source` and `destination` arguments must have ' |
no test coverage detected
searching dependent graphs…