Transform Every Value

Apply one calibration formula across a numerical table while predicting a checked value, shape, and dtype.

The same numerical rule often applies to every measurement in a table. NumPy lets us write that rule once while preserving the table's shape and axis meaning.

Write One Formula for the Complete Table

We will use a small table throughout this chapter:

Its shape is (3, 2). Axis 0 means observations, and axis 1 means sensors. The first observation contains the pair [2.0, 10.0].

Suppose a calibration rule says:

calibrated value = raw value × 0.5 + 1.0

The rule applies independently to all six measurements. NumPy can state it as one array expression:

[[ 2.  6.]
 [ 3.  8.]
 [ 4. 10.]]

Multiplication and addition are elementwise here. Each output position is computed from the input at the same position. For the first sensor in the second observation:

4.0 × 0.5 + 1.0 = 3.0

For the second sensor in that observation:

14.0 × 0.5 + 1.0 = 8.0

These two hand calculations give us evidence for one complete output row. The array expression applies the same steps to the other positions.

Q1. Calculate one calibrated value

The raw value at one position is 18.0. What value does the rule raw * 0.5 + 1.0 produce at that position?

Compute it first, then check your number.

HintFollow the written operation order

First calculate 18.0 * 0.5. Add 1.0 to that intermediate value.

SolutionApply both calibration steps

18.0×0.5=9.018.0 \times 0.5 = 9.0, then 9.0+1.0=10.09.0 + 1.0 = 10.0. The calibrated value is 10.0.

Not attempted
Review

Not marked done.

Predict What Must Stay the Same

The formula changes values, not the meaning or arrangement of their positions. Before running it, we can make three predictions:

  1. The result will have shape (3, 2).
  2. Axis 0 will still mean observations and axis 1 will still mean sensors.
  3. The result will use a floating-point dtype because both the input and the constants are floating-point values.

Inspect those claims directly:

A usual run displays float64 for both dtypes. The precise floating-point dtype can depend on how the input array was created, so the important practice is to inspect it rather than assume it.

The source also remains unchanged. The expression creates a result instead of assigning into raw:

The first pair happens to contain the same value because 2.0×0.5+1.0=2.02.0 \times 0.5 + 1.0 = 2.0. The second pair makes the distinction visible. Checking only one convenient position can therefore hide a mistake or an unchanged-looking result.

Q2. Transform and inspect the table

Complete the calibration expression. The program should preserve the (3, 2) shape and print the transformed corner values.

Editable Python

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

Ready to run.

HintKeep the formula in its stated order

Assign raw * 0.5 + 1.0 to calibrated. NumPy performs each operation at every position.

SolutionApply the rule to the array
calibrated = raw * 0.5 + 1.0

The result has shape (3, 2), first value 2.0, last value 10.0, and a floating-point dtype.

Not attempted
Review

Not marked done.

Preserve the Formula's Order

Array notation does not change arithmetic precedence. Multiplication happens before addition in this expression:

calibrated = raw * 0.5 + 1.0

Moving the addition changes the formula:

different = (raw + 1.0) * 0.5

For a raw value of 10.0, the accepted rule gives 6.0:

10.0 × 0.5 + 1.0 = 6.0

The changed rule gives 5.5:

(10.0 + 1.0) × 0.5 = 5.5

Both expressions run, return the same shape, and use a floating-point dtype. Shape and dtype checks cannot prove that the formula itself is right. A small hand calculation checks the intended numerical meaning.

This lesson uses one formula rather than surveying NumPy's arithmetic and function catalogue. The useful pattern is to state a rule, predict one value and the result structure, run it over the array, then compare the evidence with the prediction.

Q3. Distinguish two formulas

For raw_value = 4.0, which pair gives the results of the accepted rule raw_value * 0.5 + 1.0 and the changed rule (raw_value + 1.0) * 0.5, in that order?

Choose one

Select one choice, then check.

HintWrite both intermediate results

For the first rule, multiply 4.0 by 0.5 before adding. For the second, add 1.0 before multiplying.

SolutionThe operation order changes the value

The accepted rule gives 4.0×0.5+1.0=3.04.0 \times 0.5 + 1.0 = 3.0. The changed rule gives (4.0+1.0)×0.5=2.5(4.0 + 1.0) \times 0.5 = 2.5.

Not attempted
Review

Not marked done.

An elementwise array expression applies one numerical rule at every position while preserving the table's shape and axis meanings. Check one value by hand, then inspect values, shape, and dtype. The next lesson replaces one shared correction with named corrections that must align to the intended axis.

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.