Created
February 15, 2019 12:56
-
-
Save Robofied/f85fec0b6c54fea92c2ee4fc69ba42aa to your computer and use it in GitHub Desktop.
Numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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