Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
If you want to understand how to get the shape of a NumPy array, let's go through a simple tutorial.
The shape of an array is a tuple that gives you information about the number of dimensions and the size of each dimension. In NumPy, you can use the shape
attribute of an array to get its shape.
If you haven't installed NumPy, do it with:
pip install numpy
You need to import NumPy to start working with it:
import numpy as np
# Creating a 1D array arr_1d = np.array([1, 2, 3, 4, 5]) print(arr_1d.shape) # Output: (5,) indicating there's 1 dimension with 5 elements. # Creating a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr_2d.shape) # Output: (3, 3) indicating a 2x3 matrix. # Creating a 3D array arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr_3d.shape) # Output: (2, 2, 2) indicating it has two 2x2 matrices.
You can reshape an array to change its shape:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) reshaped_arr = arr.reshape(2, 4) print(reshaped_arr) # Output: # [[1 2 3 4] # [5 6 7 8]] print(reshaped_arr.shape) # Output: (2, 4)
The number of dimensions can be found using ndim
:
print(arr_1d.ndim) # Output: 1 print(arr_2d.ndim) # Output: 2 print(arr_3d.ndim) # Output: 3
You can modify the shape of an array in-place using the shape attribute:
arr = np.array([1, 2, 3, 4, 5, 6]) arr.shape = (2, 3) print(arr) # Output: # [[1 2 3] # [4 5 6]]
The shape
attribute in NumPy allows you to get and set the shape of an array. It's an essential tool when working with data in NumPy as understanding the shape of your data is crucial for many operations.
Description: In NumPy, the shape of an array refers to the dimensions (size along each axis) of the array.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Get the shape of the array array_shape = array.shape print("Array:") print(array) print("Array Shape:") print(array_shape)
Description: The dimensions of a NumPy array represent the number of axes or ranks in the array.
Code:
import numpy as np # Create a NumPy array array = np.array([1, 2, 3]) # Get the number of dimensions array_dimensions = array.ndim print("Array:") print(array) print("Number of Dimensions:") print(array_dimensions)
Description: The shape
attribute of a NumPy array provides a tuple representing the dimensions of the array.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Get the shape using the shape attribute array_shape = array.shape print("Array:") print(array) print("Array Shape:") print(array_shape)
Description: The size of an array in NumPy is the total number of elements in the array, while the shape represents its dimensions.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Get the size and shape array_size = array.size array_shape = array.shape print("Array:") print(array) print("Array Size:") print(array_size) print("Array Shape:") print(array_shape)
Description: The dimensions of a NumPy array can be accessed using various attributes, such as shape
and ndim
.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing dimensions using shape and ndim array_shape = array.shape array_dimensions = array.ndim print("Array:") print(array) print("Array Shape:") print(array_shape) print("Number of Dimensions:") print(array_dimensions)
Description: The shape of an array describes its dimensions, while the size represents the total number of elements in the array.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Get the shape and size array_shape = array.shape array_size = array.size print("Array:") print(array) print("Array Shape:") print(array_shape) print("Array Size:") print(array_size)
Description: NumPy provides functions to manipulate the shape of an array, such as reshape
and flatten
.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Reshape the array reshaped_array = array.reshape(3, 2) # Flatten the array flattened_array = array.flatten() print("Original Array:") print(array) print("Reshaped Array:") print(reshaped_array) print("Flattened Array:") print(flattened_array)
Description: The ndim
attribute of a NumPy array provides the number of dimensions or axes in the array.
Code:
import numpy as np # Create a NumPy array array = np.array([[1, 2, 3], [4, 5, 6]]) # Get the number of dimensions using ndim array_dimensions = array.ndim print("Array:") print(array) print("Number of Dimensions:") print(array_dimensions)