How to change the shape of a numpy array ?

Published: January 04, 2024

Tags: Python; Numpy;

DMCA.com Protection Status

Introduction

One common task when working with arrays is changing their shape, which can be done using various methods provided by NumPy:

Getting numpy array shape and size

Before modifying the shape of a numpy array, it is crucial to examine its current shape and the total number of elements it contains. This information is vital for ensuring proper manipulation and understanding of the array's dimensions.

Let's examine the given numpy array, which we will refer to as "data."

import numpy as np

np.random.seed(42)

data = np.random.randint(0, 100, (4,5))

First, we import the numpy library as np and set a seed value of 42 using np.random.seed(42). This ensures reproducibility in generating random numbers.

Next, we use np.random.randint(0, 100, (4,5)) to create a 4x5 array of random integers ranging from 0 to 100. This array represents our data.

By following the provided code, we can generate the desired numpy array.

array([[51, 92, 14, 71, 60],
       [20, 82, 86, 74, 74],
       [87, 99, 23,  2, 21],
       [52,  1, 87, 29, 37]])

Obtaining the shape of an array with the shape()

To determine the current shape of an array, you can utilize the numpy shape() method. For instance, by calling:

data.shape

, you will receive the following output:

(4, 5)

Obtaining the number of elements within an array using size()

To obtain the number of elements within an array, you can use the size() function. For example,

data.size

returns

20

It's important to note that there is a relationship between the shape of the array and the number of elements it contains. In this case, the array has a shape of 4 by 5, which results in a total of 20 elements.

Reshaping a numpy array

Using the reshape method

In order to change the shape of a numpy array, you can use the reshape() function. The reshape() function takes in two arguments - the desired shape and the original array.

For instance, if we have an array representing a 4x5 matrix, and we want to change its shape into a 2x10 matrix, we would use the following code:

reshaped_data = data.reshape((2,10))

The output would be:

array([[51, 92, 14, 71, 60, 20, 82, 86, 74, 74],
       [87, 99, 23,  2, 21, 52,  1, 87, 29, 37]])

This indicates that the shape of the original array has been successfully changed to a 2x10 matrix. It is important to note that when using the reshape() function, the total number of elements in the original array must match the total number of elements in the desired shape (here 4 * 5 = 2 * 10). Otherwise, an error will be raised.

Moreover, the reshape() function does not actually change the shape of the original array, but rather creates a new array with the desired shape. Therefore, if you want to permanently change the shape of an array, make sure to assign the reshaped array to a variable. Additionally, the reshape() function can also take in -1 as an argument for either the desired shape or the original array.
When -1 is used for the desired shape, it means that numpy will automatically calculate the appropriate value based on the other argument and keep its original number of dimensions. For example:

reshaped_data = data.reshape((-1,2))

The output would be:

array([[51, 92],
       [14, 71],
       [60, 20],
       [82, 86],
       [74, 74],
       [87, 99],
       [23,  2],
       [21, 52],
       [ 1, 87],
       [29, 37]])

As we observe, numpy has automatically computed the desired shape and determined that there should be 10 rows.

Please note that in certain cases, when there is no solution available, numpy may return an error. This can occur when there is a discrepancy between the requested number of columns and the number of elements present.

reshaped_data = data.reshape((-1,3))

returns

ValueError: cannot reshape array of size 20 into shape (3)

Flattening an array

In some cases, you may want to convert a multidimensional array into a one-dimensional array. This can be achieved by using the flatten method.

flattened_array = data.flatten()

The output would be:

array([51, 92, 14, 71, 60, 20, 82, 86, 74, 74, 87, 99, 23,  2, 21, 52,  1,
   87, 29, 37])

Flattening is a technique I frequently employ to organize data in a pandas dataframe. This allows for more advanced filtering capabilities. It is important to note that when flattening an array, the original shape can be restored using the reshape function. However, it is crucial to remember to save the original shape of the data.

flattened_array.reshape((4,5))

The output would be:

array([[51, 92, 14, 71, 60],
       [20, 82, 86, 74, 74],
       [87, 99, 23,  2, 21],
       [52,  1, 87, 29, 37]])

Transposing an array

Another way to change the shape of an array is by transposing it. This means that the rows become columns and vice versa. In NumPy, this can be done using the transpose method or the .T attribute.

transposed_array = data.transpose()

The output would be:

array([[51, 20, 87, 52],
       [92, 82, 99,  1],
       [14, 86, 23, 87],
       [71, 74,  2, 29],
       [60, 74, 21, 37]])

References

Links Site
reshape numpy.org
transpose numpy.org
flatten numpy.org
shape numpy.org
size numpy.org