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
Reshaping arrays is a common operation when working with data in NumPy. The reshape()
function allows you to change the shape of an array without changing its data. This tutorial will guide you through how to reshape NumPy arrays.
Importing the Necessary Library:
First and foremost, you need to import the numpy
library:
import numpy as np
Creating an Array: Let's create a 1D array of numbers from 1 to 12.
arr = np.arange(1, 13) print(arr)
Output:
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Reshaping to a 2D Array: Convert the 1D array into a 3x4 matrix:
reshaped_arr = arr.reshape(3, 4) print(reshaped_arr)
Output:
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
Total Number of Elements Must Remain the Same: The total number of elements in the array must remain unchanged after reshaping. For example, an array with 12 elements can be reshaped to shapes like (3, 4), (2, 6), (4, 3), etc., but not into shapes like (3, 5) or (4, 4).
Using -1 to Automatically Determine Size: You can use the value of -1 for one of the dimensions to let NumPy automatically calculate the size for that dimension. For instance:
arr.reshape(3, -1)
This will reshape the array into a 3x4 matrix. NumPy determines that the second dimension should be 4.
Flattening an Array:
If you want to convert a multi-dimensional array back to a 1D array, you can use the ravel()
or flatten()
function:
flat_arr = reshaped_arr.ravel()
Or:
flat_arr = reshaped_arr.flatten()
Both will produce a 1D array.
Reshaping with Higher Dimensions: You're not limited to reshaping into 2D arrays. You can reshape into 3D, 4D, etc. For instance:
arr_3d = arr.reshape(3, 2, 2) print(arr_3d)
resize()
vs. reshape()
:
If you use the resize()
method and provide a new shape that doesn't fit the number of elements in the array, it will either repeat the entries (if the new size is larger) or truncate the array (if the new size is smaller). Note that this method doesn't require the total number of elements to remain the same.
arr.reshape(4, 4) # This will produce an error
Reshaping is a powerful tool in NumPy that can be particularly useful for tasks like data preprocessing, transforming data for machine learning models, and more. Remember to always ensure that the new shape is compatible with the size of the original array.
Reshaping a NumPy array involves changing its dimensions without altering the data. This can be achieved using the numpy.reshape()
function.
import numpy as np # Create a 1D array original_array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array to a 2D matrix with 2 rows and 3 columns reshaped_array = np.reshape(original_array, (2, 3)) # Print the reshaped array print("Original Array:\n", original_array) print("Reshaped Array:\n", reshaped_array)
numpy.reshape()
:Learn how to use the numpy.reshape()
function for changing the shape of a NumPy array.
import numpy as np # Create a 1D array original_array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array to a 2D matrix with 2 rows and 3 columns reshaped_array = np.reshape(original_array, (2, 3)) # Print the reshaped array print("Original Array:\n", original_array) print("Reshaped Array:\n", reshaped_array)
Explore various examples of reshaping NumPy arrays to different dimensions.
import numpy as np # Create a 1D array original_array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array to a 2D matrix with 3 rows and 2 columns reshaped_array_1 = np.reshape(original_array, (3, 2)) # Reshape the array to a 3D tensor with 2 blocks, each having 1 row and 3 columns reshaped_array_2 = np.reshape(original_array, (2, 1, 3)) # Print the reshaped arrays print("Original Array:\n", original_array) print("Reshaped Array 1:\n", reshaped_array_1) print("Reshaped Array 2:\n", reshaped_array_2)
numpy.reshape()
function usage:Understand the usage of the numpy.reshape()
function in Python for array manipulation.
import numpy as np # Create a 1D array original_array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array to a 2D matrix with 2 rows and 3 columns reshaped_array = np.reshape(original_array, (2, 3)) # Print the reshaped array print("Original Array:\n", original_array) print("Reshaped Array:\n", reshaped_array)
A sample code demonstrating how to reshape arrays in NumPy in Python.
import numpy as np # Create a 1D array original_array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array to a 2D matrix with 2 rows and 3 columns reshaped_array = np.reshape(original_array, (2, 3)) # Print the reshaped array print("Original Array:\n", original_array) print("Reshaped Array:\n", reshaped_array)
Explore reshaping techniques for multi-dimensional arrays using NumPy.
import numpy as np # Create a 2D matrix original_matrix = np.array([[1, 2, 3], [4, 5, 6]]) # Reshape the matrix to a 1D array reshaped_array = np.reshape(original_matrix, (6,)) # Print the reshaped array print("Original Matrix:\n", original_matrix) print("Reshaped Array:\n", reshaped_array)
Learn how to flatten and reshape arrays in NumPy using different functions.
import numpy as np # Create a 2D matrix original_matrix = np.array([[1, 2, 3], [4, 5, 6]]) # Flatten the matrix to a 1D array using flatten() flattened_array = original_matrix.flatten() # Reshape the matrix to a 3x2 array using reshape() reshaped_array = np.reshape(original_matrix, (3, 2)) # Print the flattened and reshaped arrays print("Original Matrix:\n", original_matrix) print("Flattened Array:\n", flattened_array) print("Reshaped Array:\n", reshaped_array)
numpy
array manipulation: reshape
vs resize
:Understand the differences between reshape
and resize
functions in NumPy for array manipulation.
import numpy as np # Create a 1D array original_array = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array using reshape() reshaped_array = np.reshape(original_array, (2, 3)) # Resize the array using resize() resized_array = np.resize(original_array, (2, 3)) # Print the reshaped and resized arrays print("Original Array:\n", original_array) print("Reshaped Array:\n", reshaped_array) print("Resized Array:\n", resized_array)