Change the dimension of a NumPy array
0 2062
Introduction
In numerical computing with Python, manipulating the shape of arrays is a common task. NumPy, a powerful library for numerical computations, provides several methods to change the dimensions of an array without altering its data. This flexibility is essential for data preprocessing, machine learning, and scientific computing.Method 1: Using shape Attribute
The shape attribute of a NumPy array allows you to change its dimensions directly. This method modifies the original array in place.
import numpy as np
arr = np.array([1, 2, 3, 4])
print("Original array:", arr)
print("Original shape:", arr.shape)
arr.shape = (2, 2)
print("Reshaped array:\n", arr)
print("New shape:", arr.shape)
Output:
Original array: [1 2 3 4]
Original shape: (4,)
Reshaped array:
[[1 2]
[3 4]]
New shape: (2, 2)
In this example, the 1D array is reshaped into a 2x2 matrix.
Method 2: Using reshape() Function
The reshape() function returns a new array with the specified shape, leaving the original array unchanged. It is useful when you need to preserve the original array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3)
print("Original array:", arr)
print("Reshaped array:\n", reshaped_arr)
Output:
Original array: [1 2 3 4 5 6]
Reshaped array:
[[1 2 3]
[4 5 6]]
Here, the 1D array is reshaped into a 2x3 matrix.
Method 3: Using resize() Function
The resize() function changes the shape and size of the array in place. If the new size is larger, the array is filled with repeated copies of the original array.
import numpy as np
arr = np.array([1, 2, 3])
arr.resize((2, 3))
print("Resized array:\n", arr)
Output:
Resized array:
[[1 2 3]
[1 2 3]]
In this example, the array is resized to a 2x3 matrix, and the original elements are repeated to fill the new size.
Method 4: Using expand_dims() Function
The expand_dims() function adds a new axis at the specified position, increasing the dimensions of the array.
import numpy as np
arr = np.array([1, 2, 3])
expanded_arr = np.expand_dims(arr, axis=0)
print("Expanded array:\n", expanded_arr)
print("New shape:", expanded_arr.shape)
Output:
Expanded array:
[[1 2 3]]
New shape: (1, 3)
Here, a new axis is added at the beginning, transforming the 1D array into a 2D array.
Method 5: Using squeeze() Function
The squeeze() function removes axes of length one from the array, effectively reducing its dimensions.
import numpy as np
arr = np.array([[[1], [2], [3]]])
squeezed_arr = np.squeeze(arr)
print("Squeezed array:\n", squeezed_arr)
print("New shape:", squeezed_arr.shape)
Output:
Squeezed array:
[1 2 3]
New shape: (3,)
In this example, the 3D array is squeezed to a 1D array by removing the singleton dimensions.
Conclusion
Changing the dimensions of a NumPy array is a fundamental operation in numerical computing. Depending on your needs, you can use theshape attribute for in-place modifications, the reshape() function for creating new arrays, or other functions like resize(), expand_dims(), and squeeze() for more specialized reshaping tasks. Understanding these methods allows you to manipulate and analyze data effectively in Python.If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments