Select by Position

Select a scalar, observation, sensor, or rectangular region while predicting whether each axis is removed or preserved.

An array can hold many readings, but a program often needs one known part of that array. NumPy uses one position for each axis. We can therefore select a single reading, a whole observation, one sensor, or a rectangular region while keeping track of the result's shape.

We will use this small table throughout the lesson:

Its shape is (3, 2): three observations by two sensors. Axis 0 names the observation rows, and axis 1 names the sensor columns.

Select One Reading

Put the row position first and the column position second:

21.5
()

readings[1, 0] means observation at position 1, sensor at position 0. Both positions are integers, so both array axes disappear. The result is one NumPy scalar. Its shape is the empty tuple () because it has no remaining array axis.

Negative positions count from the far end of an axis. The last observation and last sensor contain 24.0, so readings[-1, -1] selects that scalar. This is useful when the position relative to the end matters. It does not change the order or meaning of the axes.

Q1. Locate one reading

What value does readings[-2, 1] select from the fixed table?

Compute it first, then check your number.

HintResolve one axis at a time

Along axis 0, -2 selects the row [21.5, 22.0]. Then inspect position 1 along that row's sensor axis.

SolutionSelect the middle row's second sensor

readings[-2, 1] is 22.0.

Not attempted
Review

Not marked done.

Select a Row or a Column

One integer removes one axis but leaves the other:

[21.5 22. ] (2,)
[18.5 21.5 19. ] (3,)

In readings[1, :], the integer selects one observation and removes the observation axis. The slice : keeps every sensor position, so the result has shape (2,).

In readings[:, 0], the slice keeps the observation axis while the integer removes the sensor axis. The result has shape (3,). The values alone do not name that remaining axis, so it helps to keep the variable name sensor and the original axis meaning nearby.

An integer and a one-position slice do not produce the same shape:

The integer 1 removes the observation axis. The slice 1:2 preserves it, with length 1. Use the slice when later code still expects an (observations, sensors) table.

Q2. Preserve the observation axis

Which expression selects only the middle observation while preserving a two-dimensional result of shape (1, 2)?

Choose one

Select one choice, then check.

HintLook for a row slice

The row selection must use a slice rather than the integer 1.

SolutionUse a one-row slice

readings[1:2, :] keeps observation positions from 1 up to, but not including, 2. Its shape is (1, 2).

Not attempted
Review

Not marked done.

Select a Rectangular Region

Use a slice on each axis to keep a rectangular part of the table:

[[21.5]
 [19. ]]
(2, 1)

The row slice 1: keeps the last two observations. The column slice :1 keeps the first sensor. Since both selections are slices, both axes remain.

Before running a positional selection, name what each position means, then predict the result's values and shape. This catches a common mistake: selecting the right numbers with the wrong layout.

Q3. Select a region and report its shape

Complete the two selections. last_sensor should contain the second sensor across all observations. lower_left should remain a two-dimensional region containing the first sensor from the last two observations.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintChoose the axis behavior deliberately

Use : for all observations and integer 1 for the second sensor. For the region, keep rows from position 1 onward and preserve the first column with the slice :1.

SolutionUse one column selection and one region

The first result has shape (3,). The second has shape (2, 1) because both of its axes were selected with slices.

Not attempted
Review

Not marked done.

One position per axis can select a scalar, row, column, or region. An integer removes its axis; a slice preserves it, even when only one position remains. The next lesson selects locations that depend on the readings themselves.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.