Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Numpy
## 1. moveaxis routine
import numpy as np
x = np.zeros((3, 4, 5))
## In this all the elements shape gets shifted in the direction of source to destonation, so for this example
## it is just like cyclic rotation in clockwise direction.
print(np.moveaxis(x, 0, -1).shape)
#[Output]:
#(7, 5, 4, 6)
print(np.moveaxis(x, -1, 0).shape)
#[Output];
#(5, 3, 4)
## In this example, last axes remain unchanged.
print(np.moveaxis(x, 0, 1).shape)
#[Output]:
#(7, 6, 5, 4)
x = np.zeros((6, 7, 5, 4))
print(np.moveaxis(x, 0, 2).shape)
#[Output]:
#(7, 5, 6, 4)
## 2. numpy.transpose
x = np.arange(4).reshape((2,2))
print(x)
#[Output]:
#array([[0, 1],
# [2, 3]])
print(np.transpose(x))
#[Output]:
#array([[0, 2],
# [1, 3]])
## In this example we doesn't set the axes=none and here we set axes-0 dimensions to axes-1, axes-1 dimensions to axes-0 etc.
x = np.ones((1, 2, 3))
print(np.transpose(x, (1, 0, 2)).shape)
#[Output]:
#(2, 1, 3)
## 3. numpy.ndarray.T
x = np.array([[1.,2.],[3.,4.]])
print(x)
#[Output]:
#array([[1., 2.],
# [3., 4.]])
print(x.T)
#[Output]:
#array([[1., 3.],
# [2., 4.]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.