Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python Numpy Array shape

by suresh

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

OUTPUT

Python Numpy Array shape 1

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

OUTPUT

Python Numpy Array shape 2

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

OUTPUT

Python Numpy Array reshape 1

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

OUTPUT

Python Numpy Array reshape 2

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

OUTPUT

Python Numpy Array reshape 3

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)

OUTPUT

Python Numpy Array resize 1

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)

OUTPUT

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

OUTPUT

Python Numpy 2D Array transpose 1

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

OUTPUT

Python Numpy 3D Array transpose 2

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

OUTPUT

Python Numpy Array swapaxes 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))

OUTPUT

Python Numpy Array swapaxes 2

Python Numpy Array flatten

The Python array flatten function collapses the given array into a one-dimensional array. This 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'))

OUTPUT

Python Numpy Array flatten 1

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

OUTPUT

Python Numpy Array flatten 2

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

OUTPUT

Python Array ravel 1

In this example, we declared a three-dimensional integer array of random numbers using a 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))

OUTPUT

Python Array ravel 2

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)

OUTPUT

Python Array squeeze

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy