Get the Shape of NumPy Array

Get the Shape of NumPy Array

If you want to understand how to get the shape of a NumPy array, let's go through a simple tutorial.

1. Introduction:

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.

2. Basic Use:

Installation:

If you haven't installed NumPy, do it with:

pip install numpy 

Importing:

You need to import NumPy to start working with it:

import numpy as np 

Create an Array and Get its Shape:

# 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. 

3. Reshaping Arrays:

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) 

4. Dimensions:

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 

5. Modifying Shape:

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

Conclusion:

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.

Examples

1. How to get the shape of an array in NumPy:

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) 

2. Python NumPy array dimensions:

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) 

3. Get array shape in NumPy using shape attribute:

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) 

4. Checking array size and shape with NumPy:

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) 

5. Accessing dimensions of arrays in NumPy:

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) 

6. NumPy array shape vs size:

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) 

7. Python NumPy array shape manipulation:

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) 

8. Getting the number of dimensions in a NumPy 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) 

More Tags

battery protractor jakarta-mail arrow-functions jasmine2.0 x86 jsf-2 libcurl visual-studio-debugging codeigniter-query-builder

More Programming Guides

Other Guides

More Programming Examples