Move axes of an array to new positions. Other axes remain in their original order. .. versionadded:: 1.11.0 Parameters ---------- a : np.ndarray The array whose axes should be reordered. source : int or sequence of int Original positions of the axes to
(a, source, destination)
| 1392 | |
| 1393 | @array_function_dispatch(_moveaxis_dispatcher) |
| 1394 | def moveaxis(a, source, destination): |
| 1395 | """ |
| 1396 | Move axes of an array to new positions. |
| 1397 | |
| 1398 | Other axes remain in their original order. |
| 1399 | |
| 1400 | .. versionadded:: 1.11.0 |
| 1401 | |
| 1402 | Parameters |
| 1403 | ---------- |
| 1404 | a : np.ndarray |
| 1405 | The array whose axes should be reordered. |
| 1406 | source : int or sequence of int |
| 1407 | Original positions of the axes to move. These must be unique. |
| 1408 | destination : int or sequence of int |
| 1409 | Destination positions for each of the original axes. These must also be |
| 1410 | unique. |
| 1411 | |
| 1412 | Returns |
| 1413 | ------- |
| 1414 | result : np.ndarray |
| 1415 | Array with moved axes. This array is a view of the input array. |
| 1416 | |
| 1417 | See Also |
| 1418 | -------- |
| 1419 | transpose : Permute the dimensions of an array. |
| 1420 | swapaxes : Interchange two axes of an array. |
| 1421 | |
| 1422 | Examples |
| 1423 | -------- |
| 1424 | >>> x = np.zeros((3, 4, 5)) |
| 1425 | >>> np.moveaxis(x, 0, -1).shape |
| 1426 | (4, 5, 3) |
| 1427 | >>> np.moveaxis(x, -1, 0).shape |
| 1428 | (5, 3, 4) |
| 1429 | |
| 1430 | These all achieve the same result: |
| 1431 | |
| 1432 | >>> np.transpose(x).shape |
| 1433 | (5, 4, 3) |
| 1434 | >>> np.swapaxes(x, 0, -1).shape |
| 1435 | (5, 4, 3) |
| 1436 | >>> np.moveaxis(x, [0, 1], [-1, -2]).shape |
| 1437 | (5, 4, 3) |
| 1438 | >>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape |
| 1439 | (5, 4, 3) |
| 1440 | |
| 1441 | """ |
| 1442 | try: |
| 1443 | # allow duck-array types if they define transpose |
| 1444 | transpose = a.transpose |
| 1445 | except AttributeError: |
| 1446 | a = asarray(a) |
| 1447 | transpose = a.transpose |
| 1448 | |
| 1449 | source = normalize_axis_tuple(source, a.ndim, 'source') |
| 1450 | destination = normalize_axis_tuple(destination, a.ndim, 'destination') |
| 1451 | if len(source) != len(destination): |
no test coverage detected