Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Slicing in 2D Arrays | Indexing and Slicing
Ultimate NumPy
course content

Зміст курсу

Ultimate NumPy

Ultimate NumPy

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

bookSlicing in 2D Arrays

Slicing in 2D and higher-dimensional arrays works similarly to slicing in 1D arrays. However, in 2D arrays, there are two axes.

If we want to perform slicing only on axis 0 to retrieve 1D arrays, the syntax remains the same: array[start:end:step]. If we want to perform slicing on the elements of these 1D arrays (axis 1), the syntax is as follows: array[start:end:step, start:end:step]. Essentially, the number of slices corresponds to the number of dimensions of an array.

Moreover, we can use slicing for one axis and basic indexing for the other axis. Let's look at an example of 2D slicing (purple squares represent the elements retrieved from slicing, and the black arrow indicates that the elements are taken in reverse order):

Here is the code for this example:

123456789101112131415
import numpy as np array_2d = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]) print(array_2d[1:]) print('-' * 15) print(array_2d[:, 0]) print('-' * 15) print(array_2d[1:, 1:-1]) print('-' * 15) print(array_2d[:-1, ::2]) print('-' * 15) print(array_2d[2, ::-1])
copy

Завдання

You are working with a 2D NumPy array that represents the scores of three students in three different subjects. The scores for each student are stored in a separate row, with each element representing the score in a specific subject. Create a slice of student_scores with the last two scores of the first student (first row) using basic indexing (positive indexing) and slicing (specify only a positive start).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

bookSlicing in 2D Arrays

Slicing in 2D and higher-dimensional arrays works similarly to slicing in 1D arrays. However, in 2D arrays, there are two axes.

If we want to perform slicing only on axis 0 to retrieve 1D arrays, the syntax remains the same: array[start:end:step]. If we want to perform slicing on the elements of these 1D arrays (axis 1), the syntax is as follows: array[start:end:step, start:end:step]. Essentially, the number of slices corresponds to the number of dimensions of an array.

Moreover, we can use slicing for one axis and basic indexing for the other axis. Let's look at an example of 2D slicing (purple squares represent the elements retrieved from slicing, and the black arrow indicates that the elements are taken in reverse order):

Here is the code for this example:

123456789101112131415
import numpy as np array_2d = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]) print(array_2d[1:]) print('-' * 15) print(array_2d[:, 0]) print('-' * 15) print(array_2d[1:, 1:-1]) print('-' * 15) print(array_2d[:-1, ::2]) print('-' * 15) print(array_2d[2, ::-1])
copy

Завдання

You are working with a 2D NumPy array that represents the scores of three students in three different subjects. The scores for each student are stored in a separate row, with each element representing the score in a specific subject. Create a slice of student_scores with the last two scores of the first student (first row) using basic indexing (positive indexing) and slicing (specify only a positive start).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

bookSlicing in 2D Arrays

Slicing in 2D and higher-dimensional arrays works similarly to slicing in 1D arrays. However, in 2D arrays, there are two axes.

If we want to perform slicing only on axis 0 to retrieve 1D arrays, the syntax remains the same: array[start:end:step]. If we want to perform slicing on the elements of these 1D arrays (axis 1), the syntax is as follows: array[start:end:step, start:end:step]. Essentially, the number of slices corresponds to the number of dimensions of an array.

Moreover, we can use slicing for one axis and basic indexing for the other axis. Let's look at an example of 2D slicing (purple squares represent the elements retrieved from slicing, and the black arrow indicates that the elements are taken in reverse order):

Here is the code for this example:

123456789101112131415
import numpy as np array_2d = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]) print(array_2d[1:]) print('-' * 15) print(array_2d[:, 0]) print('-' * 15) print(array_2d[1:, 1:-1]) print('-' * 15) print(array_2d[:-1, ::2]) print('-' * 15) print(array_2d[2, ::-1])
copy

Завдання

You are working with a 2D NumPy array that represents the scores of three students in three different subjects. The scores for each student are stored in a separate row, with each element representing the score in a specific subject. Create a slice of student_scores with the last two scores of the first student (first row) using basic indexing (positive indexing) and slicing (specify only a positive start).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Slicing in 2D and higher-dimensional arrays works similarly to slicing in 1D arrays. However, in 2D arrays, there are two axes.

If we want to perform slicing only on axis 0 to retrieve 1D arrays, the syntax remains the same: array[start:end:step]. If we want to perform slicing on the elements of these 1D arrays (axis 1), the syntax is as follows: array[start:end:step, start:end:step]. Essentially, the number of slices corresponds to the number of dimensions of an array.

Moreover, we can use slicing for one axis and basic indexing for the other axis. Let's look at an example of 2D slicing (purple squares represent the elements retrieved from slicing, and the black arrow indicates that the elements are taken in reverse order):

Here is the code for this example:

123456789101112131415
import numpy as np array_2d = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]) print(array_2d[1:]) print('-' * 15) print(array_2d[:, 0]) print('-' * 15) print(array_2d[1:, 1:-1]) print('-' * 15) print(array_2d[:-1, ::2]) print('-' * 15) print(array_2d[2, ::-1])
copy

Завдання

You are working with a 2D NumPy array that represents the scores of three students in three different subjects. The scores for each student are stored in a separate row, with each element representing the score in a specific subject. Create a slice of student_scores with the last two scores of the first student (first row) using basic indexing (positive indexing) and slicing (specify only a positive start).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 4
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt