Exercises

Practice array choice, construction, structure, selection, layout, reduction, storage, and diagnosis through one recurring table.

Q1. Contrast list and array addition

What do these two expressions produce?

Choose one

Select one choice, then check.

HintRead the container before the operator

Apply the list rule to the first expression and the NumPy rule to the second.

SolutionLists join; arrays add

The list result is [18.5, 21.5, 0.5, 0.5]. The array result is [19.0, 22.0].

Not attempted
Review

Not marked done.

Q2. Choose a constructor from the rule

We need five observation times from 0.0 through 2.0, including both endpoints. Which expression states the rule directly?

Choose one

Select one choice, then check.

HintSeparate count from step

linspace uses a requested number of values. arange uses a step and excludes its stop.

SolutionUse linspace for an endpoint-and-count rule

np.linspace(0.0, 2.0, num=5) produces five evenly spaced values and includes both endpoints under its default behavior.

Not attempted
Review

Not marked done.

Q3. Read structure and axis meaning

For the chapter table with shape (3, 2), axis 0 means observations and axis 1 means sensors. Which statement is correct?

Choose one

Select one choice, then check.

HintRead one axis at a time

The first entry belongs to axis 0; the second belongs to axis 1.

SolutionThe table has three observations and two sensors

Its ndim is 2 and its size is 3 * 2 = 6. Shape alone does not prove an exact dtype.

Not attempted
Review

Not marked done.

Q4. Predict four positional selections

Given the chapter table, enter the value of readings[1, 0], followed by the shapes of readings[1, :], readings[1:2, :], and readings[:, 1].

Answer it first, then check.

HintTreat each comma-separated index independently

The integer 1 selects and removes axis 0; 1:2 retains it with length one; : retains the full axis.

SolutionThe scalar and shapes follow the index forms

The answers are 21.5, (2,), (1, 2), and (3,).

Not attempted
Review

Not marked done.

Q5. Select values by condition

Which expression selects the values 22.0 and 24.0 from readings?

Choose one

Select one choice, then check.

HintBuild the mask from the same table

readings >= 22.0 marks the two matching positions.

SolutionIndex with the comparison result

readings[readings >= 22.0] returns a one-dimensional array containing 22.0 and 24.0.

Not attempted
Review

Not marked done.

Q6. Reshape the flat readings

Complete the code so the six values become three observations by two sensors.

Editable Python

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

Ready to run.

HintPreserve the entry count

Six entries fit a (3, 2) shape because 3 * 2 = 6.

SolutionGroup the values into three pairs
readings = flat.reshape(3, 2)
Not attempted
Review

Not marked done.

Q7. Distinguish reshape from transpose

A table currently has shape (2, 3) with sensors on axis 0 and observations on axis 1. We need observations on axis 0 and sensors on axis 1. Which operation expresses that change?

Choose one

Select one choice, then check.

HintFollow the axis labels

This is not a new grouping of flat entries. Observation and sensor axes need to trade places.

SolutionTranspose the reversed table

Use table.T. The result shape is (3, 2), with the observation and sensor axes exchanged together with their values.

Not attempted
Review

Not marked done.

Q8. Calculate one sensor mean by hand

What is the mean of the first sensor column [18.5, 21.5, 19.0], rounded to two decimal places?

Compute it first, then check your number.

HintVerify the total first

18.5 + 21.5 + 19.0 = 59.0.

SolutionDivide 59 by 3

The mean is 59.0 / 3 = 19.666..., which rounds to 19.67.

Not attempted
Review

Not marked done.

Q9. Choose the sensor reduction

Which expression returns one mean for each of the two sensors?

Choose one

Select one choice, then check.

HintChoose the axis that should disappear

The result keeps sensors, so it must combine observations.

SolutionReduce axis zero

readings.mean(axis=0) returns approximately [19.67, 22.0] with shape (2,).

Not attempted
Review

Not marked done.

Q10. Make a sensor correction independent

Complete the selection so changing the first value in corrected does not change readings.

Editable Python

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

Ready to run.

HintCopy at the selection boundary

Add .copy() to the first-column selection before changing it.

SolutionCopy the selected column
corrected = readings[:, 0].copy()
Not attempted
Review

Not marked done.

Q11. Use storage evidence

Which claim is reliable?

Choose one

Select one choice, then check.

HintKeep the reshape claim conditional

Source layout can affect whether reshaping shares storage.

SolutionUse operation-specific rules and direct evidence

Basic slices and transposes are views. Boolean selection returns independent selected data. A reshape can share or copy, so inspect it when later mutation depends on the relationship.

Not attempted
Review

Not marked done.

Q12. Repair and verify an array assumption

The function expects one row per observation and one column per sensor. Repair the flat input, compute the sensor means, and keep the verification checks.

Editable Python

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

Ready to run.

HintRestore meaning before calculating

First use flat.reshape(3, 2). Then combine observations with readings.mean(axis=0).

SolutionRepair, summarize, and check

The structural check and np.allclose then verify the repaired result.

Not attempted
Review

Not marked done.

The table now has explicit values, shape, dtype, axes, selections, layout, and storage relationships. Those structural checks prepare it for the whole-array computations in Chapter 11.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 12 exercises marked done

Review

Not marked done.