Diagnose Array Assumptions

Use the final error, values, shape, dtype, ndim, and named axes to find and verify a faulty array assumption.

Array code often fails because the program assumes a shape, dtype, index, or axis that the data does not have. Guessing at a repair can hide the first mistake. A repeatable diagnosis starts from evidence.

Use One Diagnostic Sequence

When an array operation fails or returns an implausible result, use the same sequence:

  1. Read the final error and locate the failing operation.
  2. Print the array's shape, dtype, and ndim.
  3. State what every axis is meant to represent.
  4. Reproduce the assumption with a small array whose entries are easy to see.
  5. Repair the first wrong assumption and verify both values and shape.

This method joins the debugging habits from Chapter 7 with the array vocabulary from this chapter. The extra step is semantic: a shape such as (3, 2) is not enough until we say that its axes mean observations and sensors.

Q1. Order the diagnosis

Which sequence uses evidence before changing the code?

Choose one

Select one choice, then check.

HintSeparate evidence from repair

First establish what the program received and what the operation expected. Only then change the code or data.

SolutionInspect, name, reduce, repair, verify

Read the final error, inspect shape, dtype, and ndim, name the intended axes, reproduce the issue in a small case, then repair the first wrong assumption and verify the result.

Not attempted
Review

Not marked done.

Repair a Missing Axis

Suppose a loader returns all six readings as one flat array:

The final error says that axis 1 is out of bounds for an array of dimension 1. Inspecting the array confirms the mismatch:

shape: (6,)
dtype: float64
ndim: 1

The intended data has three observations and two sensors, but flat has only one axis. Because the entry count and intended order are known, reshape the data and then repeat the reduction:

[[18.5 20. ]
 [21.5 22. ]
 [19.  24. ]]
[19.66666667 22.        ]
(2,)

The repair addresses two distinct assumptions. Reshaping restores the (observations, sensors) table. Reducing axis 0 combines observations and leaves one mean per sensor. The printed table, values, and result shape verify the intended meaning rather than merely showing that the exception is gone.

Q2. Repair and verify a flat table

Complete the program so the flat input becomes a (3, 2) observation-by-sensor table and produces one maximum per sensor.

Editable Python

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

Ready to run.

HintRestore observations and sensors

The table needs three rows and two columns. Combine the observation rows to leave two sensor maxima.

SolutionRestore the table before reducing it
Not attempted
Review

Not marked done.

Check Selection and Dtype Assumptions

The same sequence applies when selection fails. This mask has two entries:

Writing readings[sensor_mask] applies the mask to axis 0, whose length is 3. The boolean-index error reports that the mask length does not match that axis. Inspection reveals the intended repair: this two-entry mask describes sensors, which are on axis 1.

[[20.]
 [22.]
 [24.]]
(3, 1)

An integer index has a similar boundary. readings[:, 2] fails because axis 1 has size 2, so its valid positions are 0 and 1. An axis can also be invalid: readings.sum(axis=2) asks for a third axis in a two-dimensional array. In each case, shape and named axes explain the error more reliably than trying nearby numbers.

Dtype is another assumption worth inspecting. A table loaded as text may look numerical while storing strings:

The string dtype explains why a numerical mean fails. If the source contract says that every entry is a valid decimal reading, the justified repair is explicit conversion:

Conversion is not a universal fix for mixed or invalid text. It is correct here only because the data contract and the visible entries support it.

Q3. Repair a mask on the wrong axis

The mask describes the two sensors, not the three observations. Which repair selects the second sensor while preserving its axis?

Choose one

Select one choice, then check.

HintApply the mask to its semantic axis

Keep every observation with : and place the two-entry mask in the sensor position.

SolutionMask the sensor axis

Use readings[:, sensor_mask]. It keeps all three observations and selects one sensor, producing shape (3, 1).

Not attempted
Review

Not marked done.

Diagnose arrays through evidence: read the final error, inspect shape, dtype, and dimensionality, name every axis, reproduce the assumption in a small case, then repair and verify both values and shape. Removing an exception is not enough; the result must recover the intended data meaning.

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.