Compute a Weighted Score
Expand one row's products and sum, then use the same checked operation to produce one score per observation.
The centered measurement table has one row per observation and one column per sensor:
Suppose the first sensor contributes its full centered value and the second sensor contributes half of its centered value. We can record those two choices as weights:
weights = np.array([1.0, 0.5])
Our task is to produce one score for each observation. Before using compact NumPy syntax, we will make every contribution visible.
Compute One Row by Hand
The first observation is [-1.0, -2.0]. Pair each value with the weight in the
same sensor position:
sensor 0 contribution: -1.0 × 1.0 = -1.0
sensor 1 contribution: -2.0 × 0.5 = -1.0
score: -1.0 + -1.0 = -2.0
In Python, the same steps are:
contributions: [-1. -1.]
score: -2.0
Both arrays have length 2. Position 0 means sensor 0 in each array, and position 1 means sensor 1. The multiplication keeps those positions separate; the sum then combines the two contributions into one score.
Q1. Expand one weighted score
The third observation is [1.0, 2.0]. Using weights [1.0, 0.5], what are its
two contributions and final score?
Select one choice, then check.
HintKeep the positions paired
The two products are 1.0 * 1.0 and 2.0 * 0.5.
SolutionAdd the two weighted contributions
The products are 1.0 and 1.0. Their sum is 2.0.
Repeat the Same Work for Every Row
The whole table can expose all six contributions at once:
[[-1. -1.]
[ 0. 0.]
[ 1. 1.]]
[-2. 0. 2.]
Broadcasting pairs the two weights with the two sensor columns in every row.
Then sum(axis=1) combines sensors within each observation. The observation
axis remains, so the score array has shape (3,): one score for each of the
three observations.
The sizes carry meaning:
| Array | Shape | Axis meaning |
|---|---|---|
centered | (3, 2) | observations, sensors |
weights | (2,) | sensors |
all_contributions | (3, 2) | observations, sensors |
scores_from_sum | (3,) | observations |
There must be one weight for each sensor. A weight array of shape (3,) would
not describe these two sensor columns. NumPy would reject the operation rather
than inventing the missing relationship.
Q2. Check the matching size and output shape
values has shape (5, 2): five observations and two sensors. Which weight
and output shapes match the same scoring rule?
Select one choice, then check.
HintName the inner role
The two sensor positions are the values being combined. The five observation positions remain in the output.
SolutionMatch sensors and preserve observations
The weights have shape (2,), one entry per sensor. Combining each row with
those weights returns one score per observation, shape (5,).
Compact the Checked Work with @
NumPy's @ operator can express this repeated multiply-then-sum operation:
[-2. 0. 2.]
Read this particular expression from its roles and sizes:
(3 observations, 2 sensors) @ (2 sensor weights)
-> (3 observation scores)
The two sensor sizes must match because those are the positions multiplied and
summed. The three observations remain, giving output shape (3,).
@ is not a replacement for understanding the visible products. When a score
looks wrong, expand one row into its contributions and check it by hand. Later
mathematics lessons will name and study the broader structure. Here our job is
to read, run, and verify the NumPy computation.
Q3. Compare visible contributions with `@`
Complete the program so it calculates one contribution per sensor, sums each
row, computes the compact result with @, and verifies that both forms agree.
Editable Python
Ready to run.
HintWrite the expanded form first
Use centered * weights, then .sum(axis=1). The compact form is
centered @ weights.
SolutionCheck the compact form against visible work
A weighted score pairs each sensor value with one weight, makes the products,
and adds the contributions. Expand one row first, check that the sensor sizes
match, and predict one output per observation. Use @ only as a compact form
of work that remains possible to inspect.
References
- NumPy documentation:
numpy.matmul— the operation implemented by@for NumPy arrays and its matching-size rule.