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))

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))

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))

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))

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 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)

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 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))

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))

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))

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 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'))

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'))

Python 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'))

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 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)
