IKH

Ch -9

If we are not specifying the axes parameter

Table banana

Example

Python
In [236]: 
# Tranpose of 1-D array ==> no impact 
a  = np.array([10,20,30,40]) 
atrans = np.transpose(a) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}")

Output

PowerShell
Original Array :  
 [10 20 30 40] 
Transposed Array :  
 [10 20 30 40] 
Original Array shape :  (4,) 
Transposed Array shape: (4,) 

Example

Python
In [237]: 
# Transpose of 2-D  
# In 2-D array, because of transpose, rows will become columns and columns will 
become rows. 
a  = np.arange(1,7).reshape(2,3) 
atrans = np.transpose(a)
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}") 

Output

PowerShell
Original Array :  
 [[1 2 3] 
 [4 5 6]] 
Transposed Array :  
 [[1 4] 
 [2 5] 
 [3 6]] 
Original Array shape :  (2, 3) 
Transposed Array shape: (3, 2) 

3-D array shape : (2,3,4):

  • 2—>2 2-Dimensional arrays
  • 3 —>In every 2-D array, 3 rows
  • 4 —>In every 2-D array, 4 columns
  • 24—>Total number of elements

If we transpose this 3-D array then the shape will become to (4,3,2):

  • 4 —> 4 2-D arrays
  • 3 —>In every 2-D array, 3 rows
  • 2 —>In every 2-D array, 2 columns
  • 24—>Total number of elements

Example

Python
In [238]: 
# Transpose of 4-D  
a  = np.arange(1,49).reshape(2,2,3,4) 
atrans = np.transpose(a) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}")

Output

PowerShell
Original Array :  
 [[[[ 1  2  3  4] 
   [ 5  6  7  8] 
   [ 9 10 11 12]] 
 
  [[13 14 15 16] 
   [17 18 19 20] 
   [21 22 23 24]]] 
 
 
 [[[25 26 27 28] 
   [29 30 31 32] 
   [33 34 35 36]] 
 
  [[37 38 39 40] 
   [41 42 43 44] 
   [45 46 47 48]]]] 
Transposed Array :  
 [[[[ 1 25] 
   [13 37]] 
 
  [[ 5 29] 
   [17 41]] 
 
  [[ 9 33] 
   [21 45]]] 
 
 
 [[[ 2 26] 
   [14 38]] 
 
  [[ 6 30] 
   [18 42]] 
 
  [[10 34] 
   [22 46]]] 
 
 
 [[[ 3 27] 
   [15 39]] 
 
  [[ 7 31] 
 [19 43]] 
 
  [[11 35] 
   [23 47]]] 
 
 
 [[[ 4 28] 
   [16 40]] 
 
  [[ 8 32] 
   [20 44]] 
 
  [[12 36] 
   [24 48]]]] 
Original Array shape :  (2, 2, 3, 4) 
Transposed Array shape: (4, 3, 2, 2) 

by specifying the ‘axes’ parameter while calling the transpose() function

  • axes parameter describes in which order we have to take axes.
  • It is very helpful for 3-D and 4-D arrays.
  • We can specify the customized dimensions

Example

Python
In [239]: 
# 1-D array transpose() with axes parameter 
a  = np.array([10,20,30,40]) 
atrans = np.transpose(a,axes=0) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}")

Output

PowerShell
Original Array :  
 [10 20 30 40] 
Transposed Array :  
 [10 20 30 40] 
Original Array shape :  (4,) 
Transposed Array shape: (4,) 

Example

Python
In [240]: 
# 2-D array transpose() with axes parameter 
a  = np.arange(1,7).reshape(2,3) 
atrans = np.transpose(a,axes=(0,1)) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}") 

Output

PowerShell
Original Array :  
 [[1 2 3] 
 [4 5 6]] 
Transposed Array :  
 [[1 2 3] 
 [4 5 6]] 
Original Array shape :  (2, 3) 
Transposed Array shape: (2, 3) 

Example

Python
In [241]: 
# 2-D array transpose() with axes parameter 
a  = np.arange(1,7).reshape(2,3) 
atrans = np.transpose(a,axes=(1,0)) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}") 

Output

PowerShell
Original Array :  
 [[1 2 3] 
 [4 5 6]] 
Transposed Array :  
 [[1 4] 
 [2 5] 
 [3 6]] 
Original Array shape :  (2, 3) 
Transposed Array shape: (3, 2) 

Example

Python
In [242]: 
# 3-D array transpose() with axes parameter 
# (2,3,4) ==> Original Array shape 
# 2--->2-D arrays (axis-0 : 0) 
# 3--->3 rows in every 2-D array (axis-1 : 1) 
# 4--->4 columns in every 2-D array (axis-2 : 2) 
# (2,4,3) ==> Customized Transposed array shape 
 
a  = np.arange(1,25).reshape(2,3,4) 
atrans = np.transpose(a,axes=(0,2,1)) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}") 

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]] 
 
 [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
Transposed Array :  
 [[[ 1  5  9] 
  [ 2  6 10] 
  [ 3  7 11] 
  [ 4  8 12]] 
 
 [[13 17 21] 
  [14 18 22] 
  [15 19 23] 
  [16 20 24]]] 
Original Array shape :  (2, 3, 4) 
Transposed Array shape: (2, 4, 3) 

Example

Python
In [243]: 
# 3-D array transpose() with axes parameter 
# (2,3,4) ==> Original Array shape 
# 2--->2-D arrays (axis-0 : 0) 
# 3--->3 rows in every 2-D array (axis-1 : 1) 
# 4--->4 columns in every 2-D array (axis-2 : 2) 
# (3,2,4) ==> Customized Transposed array shape 
 
a  = np.arange(1,25).reshape(2,3,4) 
atrans = np.transpose(a,axes=(1,0,2)) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}")

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]] 
 
 [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
Transposed Array :  
 [[[ 1  2  3  4] 
  [13 14 15 16]] 
 
 [[ 5  6  7  8] 
  [17 18 19 20]] 
 
 [[ 9 10 11 12] 
  [21 22 23 24]]] 
Original Array shape :  (2, 3, 4) 
Transposed Array shape: (3, 2, 4)

Example

Python
In [244]: 
### Note: If we repeat the same axis multiple times then we will get error. 
b = np.transpose(a,axes=(2,2,1)) 

Output

PowerShell
--------------------------------------------------------------------------- 
ValueError                                Traceback (most recent call last) 
<ipython-input-244-31651c2bfc31> in <module> 
      1 ### Note: If we repeat the same axis multiple times then we will get 
error. ----> 2 b = np.transpose(a,axes=(2,2,1)) 
 
<__array_function__ internals> in transpose(*args, **kwargs) 
F:\Users\Gopi\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in transp
 ose(a, axes) 
    656  
    657     """ --> 658     return _wrapfunc(a, 'transpose', axes) 
    659  
    660  
 
F:\Users\Gopi\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapf
 unc(obj, method, *args, **kwds) 
     56  
     57     try: ---> 58         return bound(*args, **kwds) 
     59     except TypeError: 
     60         # A TypeError occurs if the object does have such a method in 
its 
 
ValueError: repeated axis in transpose

transpose() :: ndarray class method

  • The behaviour is exactly same as numpy library function transpose()

Example

Python
In [245]: 
import numpy as np 
help(np.ndarray.transpose) 
In [246]: 
# 3-D Array  
a  = np.arange(1,25).reshape(2,3,4) 
atrans = a.transpose((0,2,1)) 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}")

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]] 
   [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
Transposed Array :  
 [[[ 1  5  9] 
  [ 2  6 10] 
  [ 3  7 11] 
  [ 4  8 12]] 
 
 [[13 17 21] 
  [14 18 22] 
  [15 19 23] 
  [16 20 24]]] 
Original Array shape :  (2, 3, 4) 
Transposed Array shape: (2, 4, 3)

T :: variable in ndarray class

  • We can use shortcut representation of the transpose with T variable
  • ndarray_obj.T ==> it will simply reverse the dimensions
  • Customized dimensions are possible with T variable

Example

Python
In [247]: 
# By using T variable 
a  = np.arange(1,25).reshape(2,3,4) 
atrans = a.T 
print(f"Original Array : \n {a}") 
print(f"Transposed Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Transposed Array shape: {atrans.shape}")

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]] 
 
 [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
Transposed Array :  
 [[[ 1 13] 
  [ 5 17] 
  [ 9 21]]
  [[ 2 14] 
  [ 6 18] 
  [10 22]] 
 
 [[ 3 15] 
  [ 7 19] 
  [11 23]] 
 
 [[ 4 16] 
  [ 8 20] 
  [12 24]]] 
Original Array shape :  (2, 3, 4) 
Transposed Array shape: (4, 3, 2) 

reshape Vs transpose

  • In reshape we can change the size of dimension, but total size should be same.

Example

Python
input: (3,4)

Output

PowerShell
output: (4,3),(2,6),(6,2),(1,12),(12,1),(2,2,3),(3,2,2) 
  • But in transpose just we are interchanging the dimensions,but we won’t change the size of any dimension.

Example

Python
input: (3,4) ==> output: (4,3) 

Example

Python
input: (2,3,4) ==> output: (4,3,2),(2,4,3),(3,2,4),(3,4,2) but we cannot 
take(2,12),(3,8) 
  • transpose is special case of reshape

Various possible syntaxes for transpose(:

  • numpy.transpose(a)
  • numpy.transpose(a,axes=(2,0,1))
  • ndarrayobject.transpose()
  • ndarrayobject.transpose(*axes)
  • ndarrayobject.T

Note

1,3,5 lines are equal wrt functionality.

swapaxes() ==> numpy library function and ndarray class method

  • By using transpose we can interchange any number of dimensions.
  • But if we want to interchange only two dimensions then we should go for
    swapaxes() function.
  • It is the special case of transpose() function.
swapaxes() :: numpy library function

Example

Python
In [248]: 
import numpy as np 
help(np.swapaxes) 

Output

PowerShell
Help on function swapaxes in module numpy: 
 
swapaxes(a, axis1, axis2) 
    Interchange two axes of an array. 

Example

Python
In [249]: 
a = np.arange(1,7).reshape(3,2) 
aswap = np.swapaxes(a,0,1) 
print(f"Original Array : \n {a}") 
print(f"Swapped Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Swapped Array shape: {aswap.shape}")

Output

PowerShell
Original Array :  
 [[1 2] 
 [3 4] 
 [5 6]] 
Swapped Array :  
 [[[ 1 13] 
  [ 5 17] 
  [ 9 21]] 
 
 [[ 2 14] 
  [ 6 18] 
  [10 22]] 
 
 [[ 3 15] 
[ 7 19] 
  [11 23]] 
 
 [[ 4 16] 
  [ 8 20] 
  [12 24]]] 
Original Array shape :  (3, 2) 
Swapped Array shape: (2, 3) 

Example

Python
In [250]: 
# 2-D array there is no difference when we swap 
a = np.arange(1,7).reshape(3,2) 
b = np.swapaxes(a,0,1) 
c = np.swapaxes(a,1,0) 
print(f"Swapped Array b : \n {b}") 
print(f"Swapped Array c : \n {c}") 
print(f"Swapped Array b shape :  {b.shape}") 
print(f"Swapped Array c shape :  {c.shape}")

Output

PowerShell
Swapped Array b :  
 [[1 3 5] 
 [2 4 6]] 
Swapped Array c :  
 [[1 3 5] 
 [2 4 6]] 
Swapped Array b shape :  (2, 3) 
Swapped Array c shape :  (2, 3)

Example

Python
In [251]: 
# 3-D array  
a = np.arange(1,25).reshape(2,3,4) 
aswap = np.swapaxes(a,0,2)  # 0 and 2 axes values will be swapped : (4,3,2) 
print(f"Original Array : \n {a}") 
print(f"Swapped Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Swapped Array shape: {aswap.shape}") 

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]]
   [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
Swapped Array :  
 [[[ 1 13] 
  [ 5 17] 
  [ 9 21]] 
 
 [[ 2 14] 
  [ 6 18] 
  [10 22]] 
 
 [[ 3 15] 
  [ 7 19] 
  [11 23]] 
 
 [[ 4 16] 
  [ 8 20] 
  [12 24]]] 
Original Array shape :  (2, 3, 4) 
Swapped Array shape: (4, 3, 2) 

Example

Python
In [252]: 
# 3-D array  
 
a = np.arange(1,25).reshape(2,3,4) 
aswap = np.swapaxes(a,0,1)  # 0 and 1 axes values will be swapped : (3,2,4) 
print(f"Original Array : \n {a}") 
print(f"Swapped Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Swapped Array shape: {aswap.shape}")

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]] 
 
 [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
 
Swapped Array :  
 [[[ 1 13] 
  [ 5 17] 
  [ 9 21]] 
 
 [[ 2 14] 
  [ 6 18] 
  [10 22]] 
 
 [[ 3 15] 
  [ 7 19] 
  [11 23]] 
 
 [[ 4 16] 
  [ 8 20] 
  [12 24]]] 
Original Array shape :  (2, 3, 4) 
Swapped Array shape: (3, 2, 4) 

swapaxes() :: ndarray class method

  • ndarray class also contains swapaxes() method which is exactly same as numpy
  • library swapaxes() function.

Example

Python
In [253]: 
import numpy as np 
help(np.ndarray.swapaxes) 

Output

PowerShell
Help on method_descriptor: 
 
swapaxes(...) 
    a.swapaxes(axis1, axis2) 
     
    Return a view of the array with `axis1` and `axis2` interchanged. 
     
    Refer to `numpy.swapaxes` for full documentation. 
     
    See Also 
    -------- 
    numpy.swapaxes : equivalent function 

Example

Python
In [254]: 
# 3-D array 
a = np.arange(1,25).reshape(2,3,4) 
aswap = a.swapaxes(0,1)  # 0 and 1 axes values will be swapped : (3,2,4) 
print(f"Original Array : \n {a}") 
print(f"Swapped Array : \n {atrans}") 
print(f"Original Array shape :  {a.shape}") 
print(f"Swapped Array shape: {aswap.shape}") 

Output

PowerShell
Original Array :  
 [[[ 1  2  3  4] 
  [ 5  6  7  8] 
  [ 9 10 11 12]] 
 
 [[13 14 15 16] 
  [17 18 19 20] 
  [21 22 23 24]]] 
Swapped Array :  
 [[[ 1 13] 
  [ 5 17] 
  [ 9 21]] 
 
 [[ 2 14] 
  [ 6 18] 
  [10 22]] 
 
 [[ 3 15] 
  [ 7 19] 
  [11 23]] 
 
 [[ 4 16] 
  [ 8 20] 
  [12 24]]] 
Original Array shape :  (2, 3, 4) 
Swapped Array shape: (3, 2, 4)

transpose() Vs swapaxes():

  • By using tarnspose() we can interchange any number of dimensions.
  • But by using swapaxes() we can interchange only two dimensions.

Report an error