Conteúdo do Curso
Ultimate NumPy
Ultimate NumPy
Assigning Values to Indexed Subarrays
With indexed arrays, things start getting more interesting. Here we’ll focus on 1D and 2D subarrays, as 3D subarrays are rarely used in practice.
Let's first start with assigning values to slices. The general syntax looks like this: slice = values
, where slice
is a slice of a certain array and values
are the values to be assigned.
Possible formats of values
:
- a single scalar (number);
- a 1D array of the same size as the slice (if it is 1D); or the size of the second dimension (if the slice is 2D);
- a 2D array of the same shape as a 2D slice.
Here is an example to clarify all of this:
import numpy as np array_1d = np.array([1, 4, 6, 2, 9]) # Assigning an array to the slice of array_1d array_1d[1:-1] = np.array([3, 5, 7]) print(array_1d) print('-' * 12) # Assigning a scalar to the slice of array_1d array_1d[1:-1] = 5 print(array_1d) print('-' * 12) array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # Assigning a 2D array to the slice of array_2d array_2d[1:3, 1:] = np.array([[20, 21], [40, 41]]) print(array_2d) print('-' * 12) # Assigning a 1D array to the slice of array_2d array_2d[1:3, 1:] = [50, 51] print(array_2d) print('-' * 12) # Assigning a scalar to the slice of array_2d array_2d[1:3, 1:] = 30 print(array_2d)
Let’s now visualize it:
When we assign a scalar to a 1D slice, this scalar is assigned to every element of the slice. When a 1D array is assigned to a 2D slice, this 1D array is assigned to every 1D array in the slice. Assigning a scalar to a 2D slice works the same as with a 1D slice.
Assigning values to integer array indexed subarrays works the same way as with slices. Assigning values to boolean indexed subarrays works the same way as with 1D slices.
Tarefa
You are managing a dataset of product prices and ratings. The prices are stored in the prices
array, and the ratings (out of 10) are stored in the ratings
array, where each row represents a different product category. Your task is to update the prices and ratings based on specific criteria:
- Assign the value of
20
to every element inprices
greater than 10 (boolean indexing) using a scalar. - Assign a NumPy array with elements
9
,8
to the last two elements of the second row (second 1D array) ofratings
. Use a positive row index and a slice specifying onlystart
(positive index).
Obrigado pelo seu feedback!
Assigning Values to Indexed Subarrays
With indexed arrays, things start getting more interesting. Here we’ll focus on 1D and 2D subarrays, as 3D subarrays are rarely used in practice.
Let's first start with assigning values to slices. The general syntax looks like this: slice = values
, where slice
is a slice of a certain array and values
are the values to be assigned.
Possible formats of values
:
- a single scalar (number);
- a 1D array of the same size as the slice (if it is 1D); or the size of the second dimension (if the slice is 2D);
- a 2D array of the same shape as a 2D slice.
Here is an example to clarify all of this:
import numpy as np array_1d = np.array([1, 4, 6, 2, 9]) # Assigning an array to the slice of array_1d array_1d[1:-1] = np.array([3, 5, 7]) print(array_1d) print('-' * 12) # Assigning a scalar to the slice of array_1d array_1d[1:-1] = 5 print(array_1d) print('-' * 12) array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # Assigning a 2D array to the slice of array_2d array_2d[1:3, 1:] = np.array([[20, 21], [40, 41]]) print(array_2d) print('-' * 12) # Assigning a 1D array to the slice of array_2d array_2d[1:3, 1:] = [50, 51] print(array_2d) print('-' * 12) # Assigning a scalar to the slice of array_2d array_2d[1:3, 1:] = 30 print(array_2d)
Let’s now visualize it:
When we assign a scalar to a 1D slice, this scalar is assigned to every element of the slice. When a 1D array is assigned to a 2D slice, this 1D array is assigned to every 1D array in the slice. Assigning a scalar to a 2D slice works the same as with a 1D slice.
Assigning values to integer array indexed subarrays works the same way as with slices. Assigning values to boolean indexed subarrays works the same way as with 1D slices.
Tarefa
You are managing a dataset of product prices and ratings. The prices are stored in the prices
array, and the ratings (out of 10) are stored in the ratings
array, where each row represents a different product category. Your task is to update the prices and ratings based on specific criteria:
- Assign the value of
20
to every element inprices
greater than 10 (boolean indexing) using a scalar. - Assign a NumPy array with elements
9
,8
to the last two elements of the second row (second 1D array) ofratings
. Use a positive row index and a slice specifying onlystart
(positive index).
Obrigado pelo seu feedback!
Assigning Values to Indexed Subarrays
With indexed arrays, things start getting more interesting. Here we’ll focus on 1D and 2D subarrays, as 3D subarrays are rarely used in practice.
Let's first start with assigning values to slices. The general syntax looks like this: slice = values
, where slice
is a slice of a certain array and values
are the values to be assigned.
Possible formats of values
:
- a single scalar (number);
- a 1D array of the same size as the slice (if it is 1D); or the size of the second dimension (if the slice is 2D);
- a 2D array of the same shape as a 2D slice.
Here is an example to clarify all of this:
import numpy as np array_1d = np.array([1, 4, 6, 2, 9]) # Assigning an array to the slice of array_1d array_1d[1:-1] = np.array([3, 5, 7]) print(array_1d) print('-' * 12) # Assigning a scalar to the slice of array_1d array_1d[1:-1] = 5 print(array_1d) print('-' * 12) array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # Assigning a 2D array to the slice of array_2d array_2d[1:3, 1:] = np.array([[20, 21], [40, 41]]) print(array_2d) print('-' * 12) # Assigning a 1D array to the slice of array_2d array_2d[1:3, 1:] = [50, 51] print(array_2d) print('-' * 12) # Assigning a scalar to the slice of array_2d array_2d[1:3, 1:] = 30 print(array_2d)
Let’s now visualize it:
When we assign a scalar to a 1D slice, this scalar is assigned to every element of the slice. When a 1D array is assigned to a 2D slice, this 1D array is assigned to every 1D array in the slice. Assigning a scalar to a 2D slice works the same as with a 1D slice.
Assigning values to integer array indexed subarrays works the same way as with slices. Assigning values to boolean indexed subarrays works the same way as with 1D slices.
Tarefa
You are managing a dataset of product prices and ratings. The prices are stored in the prices
array, and the ratings (out of 10) are stored in the ratings
array, where each row represents a different product category. Your task is to update the prices and ratings based on specific criteria:
- Assign the value of
20
to every element inprices
greater than 10 (boolean indexing) using a scalar. - Assign a NumPy array with elements
9
,8
to the last two elements of the second row (second 1D array) ofratings
. Use a positive row index and a slice specifying onlystart
(positive index).
Obrigado pelo seu feedback!
With indexed arrays, things start getting more interesting. Here we’ll focus on 1D and 2D subarrays, as 3D subarrays are rarely used in practice.
Let's first start with assigning values to slices. The general syntax looks like this: slice = values
, where slice
is a slice of a certain array and values
are the values to be assigned.
Possible formats of values
:
- a single scalar (number);
- a 1D array of the same size as the slice (if it is 1D); or the size of the second dimension (if the slice is 2D);
- a 2D array of the same shape as a 2D slice.
Here is an example to clarify all of this:
import numpy as np array_1d = np.array([1, 4, 6, 2, 9]) # Assigning an array to the slice of array_1d array_1d[1:-1] = np.array([3, 5, 7]) print(array_1d) print('-' * 12) # Assigning a scalar to the slice of array_1d array_1d[1:-1] = 5 print(array_1d) print('-' * 12) array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # Assigning a 2D array to the slice of array_2d array_2d[1:3, 1:] = np.array([[20, 21], [40, 41]]) print(array_2d) print('-' * 12) # Assigning a 1D array to the slice of array_2d array_2d[1:3, 1:] = [50, 51] print(array_2d) print('-' * 12) # Assigning a scalar to the slice of array_2d array_2d[1:3, 1:] = 30 print(array_2d)
Let’s now visualize it:
When we assign a scalar to a 1D slice, this scalar is assigned to every element of the slice. When a 1D array is assigned to a 2D slice, this 1D array is assigned to every 1D array in the slice. Assigning a scalar to a 2D slice works the same as with a 1D slice.
Assigning values to integer array indexed subarrays works the same way as with slices. Assigning values to boolean indexed subarrays works the same way as with 1D slices.
Tarefa
You are managing a dataset of product prices and ratings. The prices are stored in the prices
array, and the ratings (out of 10) are stored in the ratings
array, where each row represents a different product category. Your task is to update the prices and ratings based on specific criteria:
- Assign the value of
20
to every element inprices
greater than 10 (boolean indexing) using a scalar. - Assign a NumPy array with elements
9
,8
to the last two elements of the second row (second 1D array) ofratings
. Use a positive row index and a slice specifying onlystart
(positive index).