To get the shape of a Python NumPy array use numpy.ndarray.shape
property. The array shape can be defined as the number of elements in each dimension and dimension is defined as a number of indices or subscripts, that can specify an individual element of an array.
To get the size of the multi-dimensional array, use an attribute called shape which returns a tuple (x,y) where x is the number of rows and y is the number of columns in the array. In this article, I will explain how to get the shape of a NumPy array by using numpy.ndarray.shape
property with examples.
1. Quick Examples of Array Shape
If you are in a hurry, below are some quick examples of how to get the shape of an array. Use the size property to get the length of the NumPy array.
# Quick examples of numpy array shape
# Example 1: Get the shape of the array
arr = np.array([[3, 6, 7, 9], [2, 4, 6, 8]])
array_shape = arr.shape
# Example 2: Creating 3 dimension array
arr = np.array([[[2, 3],[3, 4]],[[5, 6], [8, 9]]])
array_shape = arr.shape
# Example 3: Using Multi dimension
arr = np.array([1, 3, 5, 7, 9], ndmin=6)
array_shape = arr.shape
2. Get NumPy Array shape
To get the shape of a NumPy array, you can use the shape
attribute of the NumPy array object. For example, the shape
attribute is used to obtain the shape of the NumPy array arr
.
Use ndarray.shape
to get the shape of the NumPy array. This returns a tuple with each index having the number of corresponding elements. The below examples return (2,4)
which means that the arr
has 2 dimensions and each dimension has 4 elements (2 rows & 4 columns).
# Import numpy
import numpy as np
# Create a NumPy array
arr = np.array([[3, 6, 7, 9], [2, 4, 6, 8]])
print("Original array:\n", arr)
# Get the shape of the array
array_shape = arr.shape
print("Array shape:\n", array_shape)
Yields below output.
Similarly, it creates a 3-dimensional NumPy array and then prints its shape. This means the array arr
has 2 dimensions along the first axis, 2 dimensions along the second axis, and 2 dimensions along the third axis.
# Creating a 3 dimensions array
arr = np.array([[[2, 3],[3, 4]],[[5, 6], [8, 9]]])
# Get the shape of the array
array_shape = arr.shape
print("Array shape:\n", array_shape)
# Output:
# Array shape:
# (2, 2, 2)
3. Get the Shape of a Multi-Dimensional Array
To create a multi-dimensional NumPy array with ndmin=6
, which means you’re specifying that the array should have a minimum of 6 dimensions. However, the array you’ve provided contains only 1 element. When you specify ndmin=6
for such a small array, NumPy will add additional singleton dimensions to meet the specified minimum dimensions.
In this case, the array has been expanded to have 6 dimensions with a shape of (1, 1, 1, 1, 1, 5)
because of the ndmin=6
argument.
# Create multi dimensional array
arr = np.array([1, 3, 5, 7, 9], ndmin=6)
print("Original array:", arr)
# Use shape of Multi-Dimensional Array
array_shape = arr.shape
print("Shape of multi dimensional array:", array_shape)
Yields below output.
# Output:
# Original array: [[[[[[1 3 5 7 9]]]]]]
# Shape of multi dimensional array: (1, 1, 1, 1, 1, 5)
Frequently Asked Questions
To get the shape of a NumPy array, you can use the shape
attribute of the NumPy array object. For example, the shape
attribute is used to obtain the shape of the NumPy array array
.
The shape of a NumPy array is a tuple representing its dimensions. For a 2D array, the shape is (rows, columns)
. For a 3D array, the shape is (depth, rows, columns)
, and so on for higher dimensions.
The shape of a NumPy array is immutable. You cannot change the shape of an existing array. However, you can create a new array with a different shape based on the original array’s data.
A shape of (n,)
indicates a 1-dimensional array with n
elements. It’s a special case where the array has a single dimension with size n
.
To get the number of dimensions of a NumPy array, you can use the ndim
attribute. For instance, the ndim
attribute is used to obtain the number of dimensions of the NumPy array array
.
4. Conclusion
In this article, I have explained how to get the shape of a Python NumPy array by using numpy.ndarray.shape
properties with examples. This returns a tuple with the number of rows and columns in an array. The array shape can be defined as the number of elements in each dimension and dimension is defined as a number of indices or subscripts, that can specify an individual element of an array.
Happy Learning!!
Related Articles
- NumPy Array Addition
- NumPy percentile() Function
- Python NumPy Array Operations
- Get NumPy Array Length
- Python NumPy Array Copy
- NumPy convolve() Function in Python
- Python NumPy nonzero() Function
- How to Use NumPy pad() in Python
- NumPy fill() Function with Examples
- How to convert NumPy array to list
- NumPy full() Function with Examples
- How to create NumPy array in different ways?