Зміст курсу
Ultimate NumPy
Ultimate NumPy
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
Before we dive into reshaping, let’s first recap the concept of 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)
:
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)
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:
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) print('-' * 15) # Reshaping the array to a 2x2x3 3D array reshaped_array_3d = array.reshape(2, 2, 3) print(reshaped_array_3d)
Here is the visualization:
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:
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)
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))
:
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)
Завдання
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.
Дякуємо за ваш відгук!
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
Before we dive into reshaping, let’s first recap the concept of 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)
:
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)
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:
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) print('-' * 15) # Reshaping the array to a 2x2x3 3D array reshaped_array_3d = array.reshape(2, 2, 3) print(reshaped_array_3d)
Here is the visualization:
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:
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)
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))
:
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)
Завдання
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.
Дякуємо за ваш відгук!
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
Before we dive into reshaping, let’s first recap the concept of 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)
:
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)
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:
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) print('-' * 15) # Reshaping the array to a 2x2x3 3D array reshaped_array_3d = array.reshape(2, 2, 3) print(reshaped_array_3d)
Here is the visualization:
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:
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)
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))
:
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)
Завдання
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.
Дякуємо за ваш відгук!
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.
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)
:
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)
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:
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) print('-' * 15) # Reshaping the array to a 2x2x3 3D array reshaped_array_3d = array.reshape(2, 2, 3) print(reshaped_array_3d)
Here is the visualization:
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:
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)
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))
:
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)
Завдання
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.