NumPy shape in Python [3 Examples]

In this Python tutorial, I will discuss what the NumPy shape in Python is, and what is meant by shape 0 and shape 1. And, how shape is different from the reshape function in Python.

To find the shape of an array, we can use the NumPy shape in Python. The NumPy shape() function returns a Python tuple indicating the size of the array in each dimension. the np.shape[0], and np.shape[1] returns the size of the row and the size of the column respectively.

NumPy shape in Python

In NumPy, the shape of an array is a Python tuple that indicates the size of the array in each dimension. It is a property that defines the structure of a Python array, that is, the number of rows, columns, and so forth, depending on the dimensionality of the array.

Syntax: The syntax for accessing the shape attribute in Python is as follows:

array_name.shape
  • It consists of a few parameters:
    • array_name: input array, whose shapes we want to find.
    • Returns: The values of the shape function always give the length of the adjacent np.array in Python.

Example: Let’s take an example to check how to implement NumPy shape in Python

import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
shape_of_matrix = matrix.shape

print("Shape of the matrix:", shape_of_matrix)

Output: In the above example the array returns (2,6) which means that the array has 2 dimensions, and each dimension has 6 values.

Shape of the matrix: (2, 3)

Here is the Screenshot of the following given Python code:

.shape python numpy

This is the basic use of the NumPy shape in Python.

Python shape function in NumPy

  • The NumPy module provides a shape function to represent the shape and size of an array in Python. The shape of an array is the no. of elements in each dimension.
  • The shape attribute always returns a tuple, that represents the length of each dimension.

Syntax: Here is the Syntax of the Python numpy shape function

numpy.shape(arr)
  • It consists of a few parameters.
    • arr: input array
    • Returns: The values of the shape tuple give the lengths of the adjacent array dimensions.

Example: Let’s take an example to check how to implement the NumPy shape in Python.

import numpy as np
 
arr = np.array([2,3,4,5,6,7,8])
print(arr)
 
print('Array Shape = ', np.shape(arr))

Output: In the above code first, we import a NumPy library in Python and create a 1-D array, it is a tuple with one value instead of an integer value. A tuple with one element has a sequence comma.

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

Here is the Screenshot of the following given Python code:

shape function in python numpy

NumPy shape 0 Python

  • Shape[0] in np.shape is a tuple that always gives dimensions of the array in Python. The shape is a tuple that gives us an indication of the no. of dimensions in the array.
  • The shape function for NumPy arrays returns the dimensions of the Python array.
  • If Y has u rows and v columns, then Y.shape is (u,v). So Y.shape[0] is v.

Example: Let’s take an example to check how to implement numpy shape 0

import numpy as np

a = np.array([[2,3],[3,4]])
b = a.shape
print(b)
c = a.shape[0]
print(c)

Output: In the above example, we have to use the function np.shape to give:

(2, 2)
2

Here is the Screenshot of the following given Python code

shape[0] python

NumPy shape[1] Python

  • In Python NumPy, some of the functions return in the format of shape(R,1) but some return as (R,).
  • This will make matrix multiplication more complex since an explicit reshape is required.
  • Shape[1] in np.shape is a tuple that always gives dimensions of the array in Python. The shape function is a tuple that gives us an arrangement of the number of dimensions in the array.
  • If Y has w rows and z columns, then Y.shape is (w,z). So Y.shape[1] is z.

Example: Let’s take an example to check how to implement Python NumPy shape 1

import numpy as np

a = np.array([[2,3],[3,4]])
b = np.shape(a)
print('Column shape=', a.shape[1])
print('Array Shape = ', np.shape(a))

Output: In the above code, we will import a NumPy library in Python and create an array using the function numpy.array. Now we can easily use the function np.shape() and pass the value 1 as a parameter. The output will display the columns in an array.

Column shape= 2
Array Shape =  (2, 2)

Here is the Screenshot of the following given Python code

shape 1 python numpy

Python numpy shape vs reshape

  • np.reshape will copy the data if it cannot make a proper view, whereas getting the shape will raise an error instead of copying the data.
  • The np.shape function will always give a tuple of array dimensions in Python and can easily be used to change the dimensions of an array.
  • The reshape function gives a new shape to an array without changing its value. It creates a new array and does not update the original array itself.
  • The shape function always returns a tuple that tells us the length of each dimension while in the case of the reshape function, returns a new value on the existing data if possible rather than creating a full copy of the original array in Python.

Example:

import numpy as np

a = np.array([2,3,4,5])
print('Array Reshape',np.reshape(a,(2,2)))# Reshape function 

print('Array shape',np.shape(a)) # shape function

Output: In the above code, we will import a NumPy library and create an array using the function numpy. array. Now, we can use an np.reshape() function to display the new array in the form of a 2*2 matrix. Along with that, we will equate with the function and it will return the array of dimensions in the form shape.

Array Reshape [[2 3]
 [4 5]]
Array shape (4,)

Here is the Screenshot of the following given Python code:

NumPy shape in Python vs reshape

Conclusion

Understanding the NumPy shape in Python, is a fundamental skill when working with data. It enables a clearer understanding of array dimensions, which is critical when performing array operations, reshaping arrays, and utilizing broadcasting.

Mastering NumPy shapes can lead to more efficient and readable code, especially when handling complex multi-dimensional data. By taking advantage of the numerous functions provided by NumPy to manipulate shapes, one can fully leverage the power and flexibility of this essential Python library.

You may like the following Python tutorials: