Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reshaping Arrays | Commonly used NumPy Functions
course content

Course Content

Ultimate NumPy

Reshaping ArraysReshaping Arrays

Array reshaping in NumPy allows you to change the shape of an array while preserving all the elements. It is a commonly used operation in machine learning since many functions and methods of machine learning libraries require arrays to have a specific shape.

Array Shapes

Before we dive into reshaping, let’s first recap the concept of array shapes.

The shape of a NumPy array is represented as a tuple indicating the number of elements along each dimension (axis).

For example, a 1D array of length 5 has a shape of (5,), while a 2D array with 3 rows and 4 columns has a shape of (3, 4):

ndarray.reshape()

NumPy arrays have a .reshape() method for reshaping. You only need to pass the shape of the resulting array either as an integer, a tuple of integers, or integers as separate arguments.

This method doesn’t modify the array in place, but returns a new array.

Note

In fact, .reshape() returns a view of the original array, so any changes made to the reshaped array will also affect the original array.

Let’s see it in action:

Here is the visualization:

Reshaping to a 2D array

Note

The number of elements in the reshaped array must be the same as in the original array, so you cannot pass an arbitrary shape.

In our example, the number of elements in array is 12, and so is the number in our reshaped arrays (3 x 4 = 12; 2 x 2 x 3 = 12), which means that the shapes are compatible.

Reshaping with -1

In NumPy, when you use -1 in the .reshape() method, it automatically calculates the size of that dimension based on the original array's size, while keeping the total number of elements the same.

Using .reshape(-1, 1) is particularly useful in machine learning when we need to reshape a 1D array into a 2D array with one column. The number of rows in this case is equal to the number of elements (calculated automatically).

Let’s look at an example:

Reshaping with (-1, 1)

As you can see, the reshaped array is treated and stored like a 2D array with 5 rows and 1 column (shape is (5, 1)), but we still perceive it as a 1D array.

numpy.reshape()

The reshape() function in NumPy is identical to the .reshape() method, but you should pass an array as its first argument. For the shape parameter, you can pass either a tuple of integers or a single integer, e.g., np.reshape(array, (3, 4)):

Task

You have a sales_data_2021 array with simulated quarterly sales data for two products in 2021. The first 4 elements of this array are the quarterly sales for the first product, and the last 4 are the quarterly sales for the second product.

Your task is the following:

Use the correct method of sales_data_2021 to reshape it into a 2D array:

  • The first row should contain the quarterly sales for the first product;
  • The second row should contain the quarterly sales for the second product.

Everything was clear?

Section 3. Chapter 4
toggle bottom row
course content

Course Content

Ultimate NumPy

Reshaping ArraysReshaping Arrays

Array reshaping in NumPy allows you to change the shape of an array while preserving all the elements. It is a commonly used operation in machine learning since many functions and methods of machine learning libraries require arrays to have a specific shape.

Array Shapes

Before we dive into reshaping, let’s first recap the concept of array shapes.

The shape of a NumPy array is represented as a tuple indicating the number of elements along each dimension (axis).

For example, a 1D array of length 5 has a shape of (5,), while a 2D array with 3 rows and 4 columns has a shape of (3, 4):

ndarray.reshape()

NumPy arrays have a .reshape() method for reshaping. You only need to pass the shape of the resulting array either as an integer, a tuple of integers, or integers as separate arguments.

This method doesn’t modify the array in place, but returns a new array.

Note

In fact, .reshape() returns a view of the original array, so any changes made to the reshaped array will also affect the original array.

Let’s see it in action:

Here is the visualization:

Reshaping to a 2D array

Note

The number of elements in the reshaped array must be the same as in the original array, so you cannot pass an arbitrary shape.

In our example, the number of elements in array is 12, and so is the number in our reshaped arrays (3 x 4 = 12; 2 x 2 x 3 = 12), which means that the shapes are compatible.

Reshaping with -1

In NumPy, when you use -1 in the .reshape() method, it automatically calculates the size of that dimension based on the original array's size, while keeping the total number of elements the same.

Using .reshape(-1, 1) is particularly useful in machine learning when we need to reshape a 1D array into a 2D array with one column. The number of rows in this case is equal to the number of elements (calculated automatically).

Let’s look at an example:

Reshaping with (-1, 1)

As you can see, the reshaped array is treated and stored like a 2D array with 5 rows and 1 column (shape is (5, 1)), but we still perceive it as a 1D array.

numpy.reshape()

The reshape() function in NumPy is identical to the .reshape() method, but you should pass an array as its first argument. For the shape parameter, you can pass either a tuple of integers or a single integer, e.g., np.reshape(array, (3, 4)):

Task

You have a sales_data_2021 array with simulated quarterly sales data for two products in 2021. The first 4 elements of this array are the quarterly sales for the first product, and the last 4 are the quarterly sales for the second product.

Your task is the following:

Use the correct method of sales_data_2021 to reshape it into a 2D array:

  • The first row should contain the quarterly sales for the first product;
  • The second row should contain the quarterly sales for the second product.

Everything was clear?

Section 3. Chapter 4
toggle bottom row
some-alt