The Python numpy module has a shape function, which helps us to find the size of an array or matrix. Apart from this shape function, the Python numpy module has to reshape, resize, transpose, swapaxes, flatten, ravel, and squeeze functions to alter the matrix of an array to the required size.
Python numpy Array shape
The numpy module has one crucial property called the shape, and this property is to get or find its shape.
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80]) print(arr) print(np.shape(arr))
[10 20 30 40 50 60 70 80]
(8,)
Let me show one more example of the Python numpy array shape. Here, we used arrays of different sizes and then found their shape using this property.
import numpy as np x = np.array([[10, 20, 30], [40, 50, 60]]) print(x) print('x = ', np.shape(x)) print() arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) print(arr) print('arr = ', np.shape(arr)) arr1 = np.random.randint(5, 50, size = (5, 8)) print('\n-----Two Dimensional Random----') print(arr1) print('arr1 = ', 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----') print(arr2) print('arr2 = ', np.shape(arr2))
[[10 20 30]
[40 50 60]]
x = (2, 3)
[[1 2]
[3 4]
[5 6]
[7 8]]
arr = (4, 2)
-----Two Dimensional Random----
[[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]]
arr1 = (5, 8)
-----Three Dimensional----
[[[ 7 24 24 22]
[24 10 16 2]
[ 1 16 7 16]]
[[22 23 12 39]
[16 30 37 15]
[16 17 3 19]]]
arr2 = (2, 3, 4)
Python numpy Array reshape
This Python numpy Array shape 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('arr = ', np.shape(arr)) print('\n---New---') new_arr = np.reshape(arr, (6,)) print(new_arr) print('new_arr = ', np.shape(new_arr))
Output
[[10 20 30]
[40 50 60]]
arr = (2, 3)
---New---
[10 20 30 40 50 60]
new_arr = (6,)
It is another example of the 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 function to return the shape after reshaping them.
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80]) print(arr) print(np.shape(arr)) print('\n---New ---') new_arr = np.reshape(arr, (2,4)) print(new_arr) print(np.shape(new_arr)) print('\n---New ---') new_arr1 = np.reshape(arr, (4, 2)) print(new_arr1) print(np.shape(new_arr1)) print('\n---New ---') new_arr2 = np.reshape(arr, (8,1)) print(new_arr2) print(np.shape(new_arr2))
[10 20 30 40 50 60 70 80]
(8,)
---New ---
[[10 20 30 40]
[50 60 70 80]]
(2, 4)
---New ---
[[10 20]
[30 40]
[50 60]
[70 80]]
(4, 2)
---New ---
[[10]
[20]
[30]
[40]
[50]
[60]
[70]
[80]]
(8, 1)
If you don’t know or don’t want to use the second value, then you can assign -1. The 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 ---') new_arr = np.reshape(arr, (2,-1)) print(new_arr) print(np.shape(new_arr)) print('\n---New ---') new_arr1 = np.reshape(arr, (4, -1)) print(new_arr1) print(np.shape(new_arr1))
[[10 20 30 40 50 60 70 80]]
Array Shape = (1, 8)
---New ---
[[10 20 30 40]
[50 60 70 80]]
(2, 4)
---New ---
[[10 20]
[30 40]
[50 60]
[70 80]]
(4, 2)
Change the Python numpy Array shape using resize
The Python resize function is useful to resize the existing numpy 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 ---') new_arr = np.resize(arr, (4, 3)) print(new_arr) print('\n---New ---') new_arr1 = np.resize(arr, (9, 8)) print(new_arr1)
resize function to change the shape output
[1 2 3]
---New ---
[[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]]
---New ---
[[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 the Python array to a 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)
How to change the Python numpy Array shape using transpose?
The transpose function helps you to transpose the given matrix or 2D.
import numpy as np arr = np.random.randint(5, 50, size = (5, 8)) print('\n-----Two Dimensional Random ----') print(arr) print('\n-----Transposed Two Dimensional ----') print(np.transpose(arr))
Change shape using transpose function output
-----Two Dimensional Random----
[[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----
[[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]]
Change Python numpy Three Dimensional Array Shape using transpose
Let me use this transpose function to transpose a 3D
import numpy as np arr2 = np.random.randint(1, 20, size = (2, 3, 4)) print('\n-----Three Dimensional Random ----') print(arr2) print('\n-----Transposed Three Dimensional ----') print(np.transpose(arr2))
Changing shape by transposing three dimensional output
-----Three Dimensional Random ----
[[[ 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 ----
[[[ 5 1]
[17 18]
[19 6]]
[[11 13]
[14 3]
[16 1]]
[[ 8 12]
[12 5]
[ 1 12]]
[[ 7 10]
[14 18]
[17 10]]]
Change Python numpy Array shape using swapaxes
The swapaxes function is to interchange the given two axes of an array. It accepts three arguments – name, first_axis, and second_axis. Next, the numpy swapaxes function swaps 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))
swapaxes 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))
swapaxes function 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]]]
Change Python numpy Array shape using flatten
The flatten function collapses the given array into a one-dimensional. This Numpy array flatten function accepts order parameters to decide the order of flattening items.
order = {C, F, A, K} – You can use one of them, or it considers C because it is the default one. C means 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 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 = F') print(arr1.flatten('F')) print('\nflatten Result of arr1 = A') print(arr1.flatten('A')) print('\nflatten Result of arr1 = K') print(arr1.flatten('K'))
output using a 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 = F
[10 30 50 70 20 40 60 80]
flatten Result of arr1 = A
[10 20 30 40 50 60 70 80]
flatten Result of arr1 = 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 to single dimensional.
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 = F') print(arr2.flatten('F')) print('\nflatten Result of arr2 = A') print(arr2.flatten('A')) print('\nflatten Result of arr2 = K') print(arr2.flatten('K'))
flatten function to flatten or change the shape 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 = 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 = 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 = 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]
Change Python numpy Array shape using ravel
The ravel function returns a contiguous flattened one-dimensional array. The syntax of this Python Numpy ravel function is
numpy.ravel(name, order = {C, F, A, K})
This ravel function accepts order parameters to decide the order of flattening items. C means the index of the items in row-major order. F means index items in the Fortran style or column major order. If it is Fortran contiguous, A reads indexes in column major order otherwise, row-major order. K reads indexes in the order of 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'))
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 of random numbers using a Python randint function. Next, we used this ravel function to flatten this random one to a contiguous single dimensional.
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))
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]
Change Python numpy Array shape using squeeze
The squeeze function removes single-dimensional entries from the given shape. The syntax of this 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('a = ', arr.shape, a.shape) b = np.squeeze(arr, axis = 0) print(b) print('b = ', arr.shape, b.shape) arr1 = np.array([[[[11, 20], [33, 40], [55, 60], [77, 80]]]]) x = np.squeeze(arr1) print(x) print('x = ', arr1.shape, x.shape) y = np.squeeze(arr1, axis = 1) print(y) print('y = ', arr1.shape, y.shape)
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]]
a = (1, 3, 4) (3, 4)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
b = (1, 3, 4) (3, 4)
[[11 20]
[33 40]
[55 60]
[77 80]]
x = (1, 1, 4, 2) (4, 2)
[[[11 20]
[33 40]
[55 60]
[77 80]]]
y = (1, 1, 4, 2) (1, 4, 2)