Python numpy Array shape

The Python numpy module has a shape function, which helps us to find the shape or size of an array or matrix. Apart from this shape function, the Python numpy module has reshape, resize, transpose, swapaxes, flatten, ravel, and squeeze functions to alter the matrix of an array to the required shape.

Python numpy Array shape

The Python numpy module has one crucial property called shape. The Python Numpy array shape property is to get or find the shape of an array.

import numpy as np
 
arr = np.array([10, 20, 30, 40, 50, 60, 70, 80])
print(arr)
 
print('Array Shape = ', np.shape(arr))

numpy Array shape output

[10 20 30 40 50 60 70 80]
Array Shape =  (8,)

Let me show one more example of Python numpy array shape. Here, we used arrays of different sizes and then finding their shape using Python array shape property.

import numpy as np
 
x = np.array([[10, 20, 30], [40, 50, 60]])
print(x)
print('Array Shape = ', np.shape(x))
print()
 
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
print(arr)
print('Array Shape = ', np.shape(arr))
 
arr1 = np.random.randint(5, 50, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print('Array Shape = ', np.shape(arr1))
 
arr2 = np.array([[[ 7, 24, 24, 22], [24, 10, 16,  2], [ 1, 16, 7, 16]],
                 [[22, 23, 12, 39], [16, 30, 37, 15],[16, 17,  3, 19]]])
print('\n-----Three Dimensional Array----')
print(arr2)
print('Array Shape = ', np.shape(arr2))

Numpy Array shape function output

[[10 20 30]
 [40 50 60]]
Array Shape =  (2, 3)

[[1 2]
 [3 4]
 [5 6]
 [7 8]]
Array Shape =  (4, 2)

-----Two Dimensional Random Array----
[[15 41 15 28 34 39 29 38]
 [12 39 22 37 32 15 40 17]
 [48 18 23 41 43 21 10 12]
 [33 49  9 18 31 38 24 28]
 [10 46 46 10 41 37 40 21]]
Array Shape =  (5, 8)

-----Three Dimensional Array----
[[[ 7 24 24 22]
  [24 10 16  2]
  [ 1 16  7 16]]

 [[22 23 12 39]
  [16 30 37 15]
  [16 17  3 19]]]
Array Shape =  (2, 3, 4)

Python numpy Array reshape

The Python Numpy array reshape function accepts an array as the first argument and shape or matrix size as the second argument. 

import numpy as np
 
arr = np.array([[10, 20, 30], [40, 50, 60]])
print(arr)
print('Array Shape = ', np.shape(arr))
 
print('\n---New Array---')
new_arr = np.reshape(arr, (6,))
print(new_arr)
print('Array Shape = ', np.shape(new_arr))

Python Numpy array reshape function output

[[10 20 30]
 [40 50 60]]
Array Shape =  (2, 3)

---New Array---
[10 20 30 40 50 60]
Array Shape =  (6,)

It is another example of an array reshape function. Here, we used a one-dimensional array and reshaped it into different dimensions. For your reference, we are using the Python Numpy array shape function to return the array shape after reshaping them.

import numpy as np
 
arr = np.array([10, 20, 30, 40, 50, 60, 70, 80])
print(arr)
print('Array Shape = ', np.shape(arr))
 
print('\n---New Array---')
new_arr = np.reshape(arr, (2,4))
print(new_arr)
print('Array Shape = ', np.shape(new_arr))
 
print('\n---New Array---')
new_arr1 = np.reshape(arr, (4, 2))
print(new_arr1)
print('Array Shape = ', np.shape(new_arr1))
 
print('\n---New Array---')
new_arr2 = np.reshape(arr, (8,1))
print(new_arr2)
print('Array Shape = ', np.shape(new_arr2))

Python Numpy Array reshape output

[10 20 30 40 50 60 70 80]
Array Shape =  (8,)

---New Array---
[[10 20 30 40]
 [50 60 70 80]]
Array Shape =  (2, 4)

---New Array---
[[10 20]
 [30 40]
 [50 60]
 [70 80]]
Array Shape =  (4, 2)

---New Array---
[[10]
 [20]
 [30]
 [40]
 [50]
 [60]
 [70]
 [80]]
Array Shape =  (8, 1)

If you don’t know or don’t want to use the second value of the shape, then you can assign -1. The numpy array reshape function automatically picks the size and replaces it with -1.

import numpy as np
 
arr = np.array([[10, 20, 30, 40, 50, 60, 70, 80]])
print(arr)
print('Array Shape = ', np.shape(arr))
 
print('\n---New Array---')
new_arr = np.reshape(arr, (2,-1))
print(new_arr)
print('Array Shape = ', np.shape(new_arr))
 
print('\n---New Array---')
new_arr1 = np.reshape(arr, (4, -1))
print(new_arr1)
print('Array Shape = ', np.shape(new_arr1))

Python Numpy Array reshape functions output

[[10 20 30 40 50 60 70 80]]
Array Shape =  (1, 8)

---New Array---
[[10 20 30 40]
 [50 60 70 80]]
Array Shape =  (2, 4)

---New Array---
[[10 20]
 [30 40]
 [50 60]
 [70 80]]
Array Shape =  (4, 2)

Python numpy Array resize

The Python array resize function is useful to resize the existing array to the desired shape. This function accepts an array as the first argument and the desired shape size as a second argument. If you specify the desired shape larger than the original, the Numpy array resize function replicates the values in the base array to create a big array.

import numpy as np
 
arr = np.array([1, 2, 3])
print(arr)
 
print('\n---New Array---')
new_arr = np.resize(arr, (4, 3))
print(new_arr)
 
print('\n---New Array---')
new_arr1 = np.resize(arr, (9, 8))
print(new_arr1)

numpy Array resize function to change the shape output

[1 2 3]

---New Array---
[[1 2 3]
 [1 2 3]
 [1 2 3]
 [1 2 3]]

---New Array---
[[1 2 3 1 2 3 1 2]
 [3 1 2 3 1 2 3 1]
 [2 3 1 2 3 1 2 3]
 [1 2 3 1 2 3 1 2]
 [3 1 2 3 1 2 3 1]
 [2 3 1 2 3 1 2 3]
 [1 2 3 1 2 3 1 2]
 [3 1 2 3 1 2 3 1]
 [2 3 1 2 3 1 2 3]]

Let us see what happens when we resize Python array to the smaller size.

import numpy as np
 
arr = np.array([1, 2, 3, 4, 5])
print('---Original Array---')
print(arr)
 
print('\n---New Array---')
new_arr = np.resize(arr, (1, 3))
print(new_arr)
 
print('\n---New Array---')
new_arr1 = np.resize(arr, (4, 1))
print(new_arr1)
Python Numpy Array resize 2

Python numpy Array transpose

The Python transpose function helps you to transpose the given matrix or 2D array.

import numpy as np
 
arr = np.random.randint(5, 50, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr)
 
print('\n-----Transposed Two Dimensional Array----')
print(np.transpose(arr))

Change numpy Array shape using transpose function output


-----Two Dimensional Random Array----
[[11 28 30 26  8 39 26 46]
 [16 22 27  9 25 16 17 10]
 [20 42 14 36 31 22 43 10]
 [41 35 25 13 36 33 18 23]
 [21 46 42 39 48  9 21 44]]

-----Transposed Two Dimensional Array----
[[11 16 20 41 21]
 [28 22 42 35 46]
 [30 27 14 25 42]
 [26  9 36 13 39]
 [ 8 25 31 36 48]
 [39 16 22 33  9]
 [26 17 43 18 21]
 [46 10 10 23 44]]

Python Transpose Three Dimensional Array

Let me use this Python array transpose function to transpose a 3D array

import numpy as np
 
arr2 = np.random.randint(1, 20, size = (2, 3, 4))
print('\n-----Three Dimensional Random Array----')
print(arr2)

print('\n-----Transposed Three Dimensional Array----')
print(np.transpose(arr2))

Changing Numpy Array shape by transposing three dimensional array output


-----Three Dimensional Random Array----
[[[ 5 11  8  7]
  [17 14 12 14]
  [19 16  1 17]]

 [[ 1 13 12 10]
  [18  3  5 18]
  [ 6  1 12 10]]]

-----Transposed Three Dimensional Array----
[[[ 5  1]
  [17 18]
  [19  6]]

 [[11 13]
  [14  3]
  [16  1]]

 [[ 8 12]
  [12  5]
  [ 1 12]]

 [[ 7 10]
  [14 18]
  [17 10]]]

Python numpy Array swapaxes

The Python array swapaxes function is to interchange the given two axes of an array. It accepts three arguments – array_name, first_axis, and second_axis. Next, the Python numpy array swapaxes function swap the first_axis and second_axis.

import numpy as np
 
arr1 = np.array([[10, 20, 30, 40]])
print(arr1)
print('swapaxes Result of arr1')
print(np.swapaxes(arr1, 0, 1))
 
arr2 = np.array([[10, 20, 30], [40, 50, 60]])
print(arr2)
print('swapaxes Result of arr2')
print(np.swapaxes(arr2, 0, 1))
 
arr3 = np.array([[[10, 20], [30, 40], [50, 60]]])
print('swapaxes Result of arr3')
print(np.swapaxes(arr3, 0, 1))
 
print('swapaxes Result of arr3 - 0, 2')
print(np.swapaxes(arr3, 0, 2))
 
print('swapaxes Result of arr3 - 2, 1')
print(np.swapaxes(arr3, 2, 1))

numpy Array swapaxes shape output

[[10 20 30 40]]
swapaxes Result of arr1
[[10]
 [20]
 [30]
 [40]]
[[10 20 30]
 [40 50 60]]
swapaxes Result of arr2
[[10 40]
 [20 50]
 [30 60]]
swapaxes Result of arr3
[[[10 20]]

 [[30 40]]

 [[50 60]]]
swapaxes Result of arr3 - 0, 2
[[[10]
  [30]
  [50]]

 [[20]
  [40]
  [60]]]
swapaxes Result of arr3 - 2, 1
[[[10 30 50]
  [20 40 60]]]

This time, we use this Python swapaxes function on a three-dimensional random array generated by the randint function.

import numpy as np
 
arr3 = np.random.randint(1, 40, size = (2, 3, 4))
print(arr3)
print('swapaxes Result of arr3, 0, 1')
print(np.swapaxes(arr3, 0, 1))
 
print('swapaxes Result of arr3, 0, 2')
print(np.swapaxes(arr3, 0, 2))
 
print('swapaxes Result of arr3, 2, 1')
print(np.swapaxes(arr3, 2, 1))

Python Numpy Array swapaxes function to shape output

[[[22 38 11 37]
  [33 33 37 30]
  [37 29 17 13]]

 [[26 17  8 35]
  [38 28 28 32]
  [ 7 38 15 26]]]
swapaxes Result of arr3, 0, 1
[[[22 38 11 37]
  [26 17  8 35]]

 [[33 33 37 30]
  [38 28 28 32]]

 [[37 29 17 13]
  [ 7 38 15 26]]]
swapaxes Result of arr3, 0, 2
[[[22 26]
  [33 38]
  [37  7]]

 [[38 17]
  [33 28]
  [29 38]]

 [[11  8]
  [37 28]
  [17 15]]

 [[37 35]
  [30 32]
  [13 26]]]
swapaxes Result of arr3, 2, 1
[[[22 33 37]
  [38 33 29]
  [11 37 17]
  [37 30 13]]

 [[26 38  7]
  [17 28 38]
  [ 8 28 15]
  [35 32 26]]]

Python numpy Array flatten

The Python array flatten function collapses the given array into a one-dimensional array. This Python Numpy array flatten function accepts order parameters to decide the order of flattening array items. 

order = {C, F, A, K} – You can use one of them, or it considers C because it is the default one. C means array items will flatten in row-major order. F means Fortran style or column major order. If the array is Fortran contiguous, A flattens in column major order otherwise, row-major order. K flattens in the order of array elements occurred in memory. 

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
print('flatten Result of arr Order C = ', arr.flatten())
print('flatten Result of arr Order F = ', arr.flatten('F'))
print('flatten Result of arr Order A = ', arr.flatten('A'))
print('flatten Result of arr Order K = ', arr.flatten('K'))
 
arr1 = np.array([[10, 20], [30, 40], [50, 60], [70, 80]])
print('\nflatten Result of arr1 Order C')
print(arr1.flatten())
 
print('\nflatten Result of arr1 - Order = F')
print(arr1.flatten('F'))
 
print('\nflatten Result of arr1 - Order = A')
print(arr1.flatten('A'))
 
print('\nflatten Result of arr1 - Order = K')
print(arr1.flatten('K'))

shape output using a numpy Array flatten function

flatten Result of arr Order C =  [1 2 3 4 5 6]
flatten Result of arr Order F =  [1 4 2 5 3 6]
flatten Result of arr Order A =  [1 2 3 4 5 6]
flatten Result of arr Order K =  [1 2 3 4 5 6]

flatten Result of arr1 Order C
[10 20 30 40 50 60 70 80]

flatten Result of arr1 - Order = F
[10 30 50 70 20 40 60 80]

flatten Result of arr1 - Order = A
[10 20 30 40 50 60 70 80]

flatten Result of arr1 - Order = K
[10 20 30 40 50 60 70 80]

This time, we generated a random three-dimensional integer array using a randint function. Next, we used this Python Numpy flatten function to flatten the array to single dimensional array.

import numpy as np
 
arr2 = np.random.randint(1, 40, size = (2, 3, 4))
print(arr2)
print('flatten Result of arr2')
print(arr2.flatten())
 
print('\nflatten Result of arr2 - Order = F')
print(arr2.flatten('F'))
 
print('\nflatten Result of arr2 - Order = A')
print(arr2.flatten('A'))
 
print('\nflatten Result of arr2 - Order = K')
print(arr2.flatten('K'))

Numpy flatten function to flatten or change the shape of an array output

[[[38 14 31 31]
  [14 16 34 21]
  [ 6 11 37 33]]

 [[11 17  7 33]
  [31 34 30 28]
  [36 14 39  8]]]
flatten Result of arr2
[38 14 31 31 14 16 34 21  6 11 37 33 11 17  7 33 31 34 30 28 36 14 39  8]

flatten Result of arr2 - Order = F
[38 11 14 31  6 36 14 17 16 34 11 14 31  7 34 30 37 39 31 33 21 28 33  8]

flatten Result of arr2 - Order = A
[38 14 31 31 14 16 34 21  6 11 37 33 11 17  7 33 31 34 30 28 36 14 39  8]

flatten Result of arr2 - Order = K
[38 14 31 31 14 16 34 21  6 11 37 33 11 17  7 33 31 34 30 28 36 14 39  8]

Python Numpy Array ravel

The Python array ravel function returns a contiguous flattened one-dimensional array. The syntax of this Python Numpy ravel function is

numpy.ravel(array_name, order = {C, F, A, K})

This Python array ravel function accepts order parameters to decide the order of flattening array items. C means the index of the array items in row-major order. F means index items in the Fortran style or column major order. If an array is Fortran contiguous, A reads indexes in column major order otherwise, row-major order. K reads array indexes in the order of array elements that occurred in memory.

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
print('arr ravel Result of Order C = ', np.ravel(arr))
print('arr ravel Result of Order F = ', np.ravel(arr, order = 'F'))
print('arr ravel Result of Order A = ', np.ravel(arr, order = 'A'))
print('arr ravel Result of Order K = ', np.ravel(arr, order = 'K'))
 
arr1 = np.array([[11, 20], [33, 40], [55, 60], [77, 80]])
print('\narr1 ravel Result of Order C = ', np.ravel(arr1))
print('arr1 ravel Result of Order F = ', np.ravel(arr1, order = 'F'))
print('arr1 ravel Result of Order A = ', np.ravel(arr1, order = 'A'))
print('arr1 ravel Result of Order K = ', np.ravel(arr1, order = 'K'))

Numpy Array ravel function to shape array output

arr ravel Result of Order C =  [1 2 3 4 5 6]
arr ravel Result of Order F =  [1 4 2 5 3 6]
arr ravel Result of Order A =  [1 2 3 4 5 6]
arr ravel Result of Order K =  [1 2 3 4 5 6]

arr1 ravel Result of Order C =  [11 20 33 40 55 60 77 80]
arr1 ravel Result of Order F =  [11 33 55 77 20 40 60 80]
arr1 ravel Result of Order A =  [11 20 33 40 55 60 77 80]
arr1 ravel Result of Order K =  [11 20 33 40 55 60 77 80]

In this example, we declared a three dimensional integer array of random numbers using a Python randint function. Next, we used this Python Numpy ravel function to flatten this random array to contiguous single dimensional array.

import numpy as np
 
arr = np.random.randint(1, 40, size = (2, 3, 4))
print(arr)
 
print('\narr ravel Result of Order C')
print(np.ravel(arr))
 
print('arr ravel Result of Order F')
print(np.ravel(arr, order = 'F'))
 
print('arr ravel Result of Order A')
print(np.ravel(arr, order = 'A'))
 
print('arr ravel Result of Order K')
print(np.ravel(arr, order = 'K'))
 
print('\nTransposed arr ravel Result of Order C')
print(np.ravel(arr.T))

Python Numpy ravel function output

[[[ 7  9 22 11]
  [ 4  8 11 35]
  [ 3 34 28  5]]

 [[28 18 18 15]
  [14 37  8 23]
  [35 35 13 23]]]

arr ravel Result of Order C
[ 7  9 22 11  4  8 11 35  3 34 28  5 28 18 18 15 14 37  8 23 35 35 13 23]
arr ravel Result of Order F
[ 7 28  4 14  3 35  9 18  8 37 34 35 22 18 11  8 28 13 11 15 35 23  5 23]
arr ravel Result of Order A
[ 7  9 22 11  4  8 11 35  3 34 28  5 28 18 18 15 14 37  8 23 35 35 13 23]
arr ravel Result of Order K
[ 7  9 22 11  4  8 11 35  3 34 28  5 28 18 18 15 14 37  8 23 35 35 13 23]

Transposed arr ravel Result of Order C
[ 7 28  4 14  3 35  9 18  8 37 34 35 22 18 11  8 28 13 11 15 35 23  5 23]

Python Array squeeze

The Python array squeeze function removes single-dimensional entries from the given array shape.The syntax of this Python Numpy squeeze function is

numpy.squeeze(array_name, axis = {None, 0, 1, 2 ..}).
import numpy as np
 
arr = np.arange(12).reshape(1, 3, 4)
print(arr)
print('\narr squeeze Result')
 
a = np.squeeze(arr)
print(a)
print('Shapes = ', arr.shape, a.shape)
 
b = np.squeeze(arr, axis = 0)
print(b)
print('Shapes = ', arr.shape, b.shape)
 
arr1 = np.array([[[[11, 20], [33, 40], [55, 60], [77, 80]]]])
x = np.squeeze(arr1)
print(x)
print('Shapes = ', arr1.shape, x.shape)
 
y = np.squeeze(arr1, axis = 1)
print(y)
print('Shapes = ', arr1.shape, y.shape)

Python Numpy Array squeeze function output

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]]

arr squeeze Result
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Shapes =  (1, 3, 4) (3, 4)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Shapes =  (1, 3, 4) (3, 4)
[[11 20]
 [33 40]
 [55 60]
 [77 80]]
Shapes =  (1, 1, 4, 2) (4, 2)
[[[11 20]
  [33 40]
  [55 60]
  [77 80]]]
Shapes =  (1, 1, 4, 2) (1, 4, 2)