MCPcopy
hub / github.com/numpy/numpy / moveaxis

Function moveaxis

numpy/_core/numeric.py:1489–1555  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1487
1488@array_function_dispatch(_moveaxis_dispatcher)
1489def 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 '

Callers 2

_multi_svd_normFunction · 0.90
crossFunction · 0.85

Calls 3

normalize_axis_tupleFunction · 0.85
asarrayFunction · 0.70
transposeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…