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
Ultimate NumPy
course content

Contenido del Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Reshaping 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

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):

1234
import numpy as np array_1d = np.array([5, 7, 1, 10, 9]) array_2d = np.zeros((3, 4)) print(array_1d.shape, array_2d.shape)
copy

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.

123456789
import numpy as np # Creating a 1D array from 0 to 11 inclusive array = np.arange(12) # Reshaping the array to a 3x4 2D array (matrix) reshaped_array_2d = array.reshape(3, 4) print(reshaped_array_2d) # Reshaping the array to a 2x2x3 3D array reshaped_array_3d = array.reshape(2, 2, 3) print(reshaped_array_3d)
copy

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, reshaping the array into a shape of 3 rows and 4 columns (3 x 4) or into a shape of 2 blocks, each containing 2 rows and 3 columns (2 x 2 x 3) still results in a total of 12 elements.

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).

123456
import numpy as np # Creating a 1D array from 0 to 4 inclusive array = np.arange(5) # Reshaping the array to a 2D array with one column reshaped_array = array.reshape(-1, 1) print(reshaped_array)
copy

The reshaped array is stored as a 2D array with 5 rows and 1 column, having the shape (5, 1). In contrast, the initial 1D array has the shape (5,), which is a tuple with a single element. For any 1D array, the shape is always (n,), where n represents the number of elements.

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)):

123456
import numpy as np # Creating a 1D array from 0 to 11 inclusive array = np.arange(12) # Reshaping the array to a 3x4 2D array (matrix) reshaped_array_2d = np.reshape(array, (3, 4)) print(reshaped_array_2d)
copy
Tarea
test

Swipe to show code editor

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

  1. Use the appropriate method of sales_data_2021 to reshape it into a 2D array.

  2. The first row should contain the quarterly sales for the first product.

  3. The second row should contain the quarterly sales for the second product.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
toggle bottom row

book
Reshaping 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

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):

1234
import numpy as np array_1d = np.array([5, 7, 1, 10, 9]) array_2d = np.zeros((3, 4)) print(array_1d.shape, array_2d.shape)
copy

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.

123456789
import numpy as np # Creating a 1D array from 0 to 11 inclusive array = np.arange(12) # Reshaping the array to a 3x4 2D array (matrix) reshaped_array_2d = array.reshape(3, 4) print(reshaped_array_2d) # Reshaping the array to a 2x2x3 3D array reshaped_array_3d = array.reshape(2, 2, 3) print(reshaped_array_3d)
copy

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, reshaping the array into a shape of 3 rows and 4 columns (3 x 4) or into a shape of 2 blocks, each containing 2 rows and 3 columns (2 x 2 x 3) still results in a total of 12 elements.

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).

123456
import numpy as np # Creating a 1D array from 0 to 4 inclusive array = np.arange(5) # Reshaping the array to a 2D array with one column reshaped_array = array.reshape(-1, 1) print(reshaped_array)
copy

The reshaped array is stored as a 2D array with 5 rows and 1 column, having the shape (5, 1). In contrast, the initial 1D array has the shape (5,), which is a tuple with a single element. For any 1D array, the shape is always (n,), where n represents the number of elements.

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)):

123456
import numpy as np # Creating a 1D array from 0 to 11 inclusive array = np.arange(12) # Reshaping the array to a 3x4 2D array (matrix) reshaped_array_2d = np.reshape(array, (3, 4)) print(reshaped_array_2d)
copy
Tarea
test

Swipe to show code editor

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

  1. Use the appropriate method of sales_data_2021 to reshape it into a 2D array.

  2. The first row should contain the quarterly sales for the first product.

  3. The second row should contain the quarterly sales for the second product.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt