How to Reshape NumPy Arrays (multiple approaches)

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python, providing support for large, multi-dimensional arrays and matrices, as well as a host of functions to perform operations on these data structures. Reshaping arrays is an essential operation in data manipulation and preparation for various computational tasks including machine learning, data analysis, and graph plotting. Understanding how to reshape arrays effectively can therefore significantly enhance data handling within Python.

Understanding NumPy Array Shapes

Before diving into reshaping, it is essential to understand the shape of a NumPy array. The shape of an array is a tuple that indicates the size of the array in each dimension. Let’s start with a basic example: creating a simple array and examining its shape.

import numpy as np

# Create a 1D array
arr = np.array([1, 2, 3, 4, 5, 6])
print('Array:', arr)
print('Shape:', arr.shape)

The above code will output:

Array: [1 2 3 4 5 6] Shape: (6,)

This output indicates that ‘arr’ is a one-dimensional array with six elements. Now, let’s see how to transform this array into different shapes.

Using the reshape() Function

The reshape() function is used to give a new shape to an array without changing its data. Here’s how you can use it:


# Reshape the 1D array to a 2x3 array
arr_2d = arr.reshape(2, 3)
print('Reshaped Array:\n', arr_2d)

The output will be:

Reshaped Array: [[1 2 3] [4 5 6]]

When reshaping an array, the new shape must contain the same number of elements as the old shape. The product of the dimensions of the new shape must be equal to the product of the dimensions of the old shape.

For instance:


# This will fail because 4 * 2 doesn't equal 6
arr_reshape_fail = arr.reshape(4, 2)

Trying to execute the above code will result in an error because you cannot reshape a 6-element array into a structure that holds anything other than 6 elements.

The -1 Wildcard

You can use the special value -1 in one of the dimensions when reshaping, and NumPy will calculate this number for you. This is particularly useful when you do not know the dimension explicitly or want NumPy to determine it automatically.


# Use -1 to automatically determine the size of the new dimension
arr_auto = arr.reshape(-1, 3)
print('Reshaped with -1:', arr_auto)

The output will display an array of shape (2, 3):

Reshaped with -1: [[1 2 3] [4 5 6]]

Reshaping to a 3D array

Reshaping an array to three dimensions is similar:


# Reshape to a 3D array
three_d_arr = arr.reshape(2, 3, 1)
print('3D Array:\n', three_d_arr)

The resulting 3D array’s output would look like this:

3D Array: [[[1] [2] [3]] [[4] [5] [6]]]

You have now created a 3D array with 2 layers, 3 rows, and 1 column in each layer.

Reshaping with np.newaxis

You can expand the dimensions of an array by inserting a new axis using np.newaxis. For example, converting a 1D array to a column vector:


# Convert 1D array to column vector
column_vector = arr[:, np.newaxis]
print('Column vector:\n', column_vector)

Your array has become a 2D array with 6 rows and 1 column, as shown here:

Column vector: [[1] [2] [3] [4] [5] [6]]

Flattening an Array

Conversely, if you need to flatten a multi-dimensional array into 1D, you have various options like flatten() or ravel() functions:

# Flatten a 2D array to 1D
flat_arr = arr_2d.flatten()
print('Flattened Array:', flat_arr)

# Alternatively
flat_arr_2 = arr_2d.ravel()
print('Flattened with ravel:', flat_arr_2)

Both methods will output the original 1D array:

Flattened Array: [1 2 3 4 5 6] 
Flattened with ravel: [1 2 3 4 5 6]

Advanced Reshaping with np.reshape

To further illustrate the versatility of np.reshape, let’s explore an example involving a more complex reshaping operation.

# Reshape a 4x4 array into a 2x2x4 3D array
arr_complex = np.arange(1, 17).reshape(4, 4)
print('Original 4x4 Array:\n', arr_complex)

arr_3d_complex = arr_complex.reshape(2, 2, 4)
print('Complex 3D reshaped array:\n', arr_3d_complex)

The output will show the complexity of the reshaped array:

Original 4x4 Array: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] Complex 3D reshaped array: [[[ 1 2 3 4] [ 5 6 7 8]] [[ 9 10 11 12] [13 14 15 16]]]

Reshape and Resize

Whereas reshape() lets you change the shape without altering the data, the resize function, both as a method and a standalone NumPy function, changes the total size of the array.


# Using resize to change the total array size
arr.resize(3, 3)
print('Resized Array:\n', arr)

This would change the size of the array to a 3×3 and append zeros to fill the new elements:

Resized Array: [[1 2 3] [4 5 6] [0 0 0]]

Conclusion

Learning how to reshape NumPy arrays is an essential skill for anyone who needs to work with data in Python. The ability to manipulate array structures allows for more efficient data processing, cleaning, and transformation to fit the requirements of various algorithms. With careful application of the described methods, developers can prepare their data for whatever comes next in their analysis or application.