Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Assigning Values to Indexed Subarrays | Indexing and Slicing
Ultimate NumPy

Assigning Values to Indexed SubarraysAssigning 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:

Let’s now visualize it:

Assigning values to a 1D slice
Assigning values to a 2D slice

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.

Завдання

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:

  1. Assign the value of 20 to every element in prices greater than 10 (boolean indexing) using a scalar.
  2. Assign a NumPy array with elements 9, 8 to the last two elements of the second row (second 1D array) of ratings. Use a positive row index and a slice specifying only start (positive index).

Все було зрозуміло?

Секція 2. Розділ 11
toggle bottom row
course content

Зміст курсу

Ultimate NumPy

Assigning Values to Indexed SubarraysAssigning 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:

Let’s now visualize it:

Assigning values to a 1D slice
Assigning values to a 2D slice

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.

Завдання

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:

  1. Assign the value of 20 to every element in prices greater than 10 (boolean indexing) using a scalar.
  2. Assign a NumPy array with elements 9, 8 to the last two elements of the second row (second 1D array) of ratings. Use a positive row index and a slice specifying only start (positive index).

Все було зрозуміло?

Секція 2. Розділ 11
toggle bottom row
some-alt