Reductions
Sum and Mean
Sum a 6-element list in a single accumulator loop, then divide by the count to
get the mean. The trace shows total growing with each addition before mean
is computed in one final step.
By hand
Initialize total to 0, then loop over values adding each element. After
the loop divide by len(values) and round to two decimal places.
naive.py
Replay: real traced execution (multi-file project)
values = [2, 5, 1, 8, 3, 5]
total = 0
for v in values:
total += v
mean = round(total / len(values), 2)
print('RESULT:', (total, mean))
values ← [2, 5, 1, 8, 3, 5]
1values = [2, 5, 1, 8, 3, 5]2total = 0values this step[2, 5, 1, 8, 3, 5]valuestotal ← 0
1values = [2, 5, 1, 8, 3, 5]2total = 03for v in values:values this step0totalv ← 2
2total = 03for v in values:4 total += vvalues this step2vtotal ← 2
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step0 → 2totalv ← 5
2total = 03for v in values:4 total += vvalues this step2 → 5vtotal ← 7
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step2 → 7totalv ← 1
2total = 03for v in values:4 total += vvalues this step5 → 1vtotal ← 8
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step7 → 8totalv ← 8
2total = 03for v in values:4 total += vvalues this step1 → 8vtotal ← 16
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step8 → 16totalv ← 3
2total = 03for v in values:4 total += vvalues this step8 → 3vtotal ← 19
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step16 → 19totalv ← 5
2total = 03for v in values:4 total += vvalues this step3 → 5vtotal ← 24
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step19 → 24totalfor v in values:
2total = 03for v in values:4 total += vmean ← 4.0
4 total += v5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))values this step4.0meanstdout ← RESULT: (24, 4.0)
5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))values this stepRESULT: (24, 4.0)stdout
With NumPy
a.sum() and a.mean() are reduction methods that collapse the entire array
to a scalar. The snapshot shows the input array's shape and dtype, then the
scalar results.
library.py
import numpy as np
values = [2, 5, 1, 8, 3, 5]
a = np.array(values)
total = int(a.sum())
mean = round(float(a.mean()), 2)
print('shape:', a.shape)
print('dtype:', a.dtype)
print('sum:', total)
print('mean:', mean)
print('RESULT:', (total, mean))
shape: (6,)
dtype: int64
sum: 24
mean: 4.0
RESULT: (24, 4.0)
Implementation notes
- A reduction collapses an array along one or more axes to a smaller shape.
Both
sum()andmean()called without arguments reduce the entire array to a single scalar. a.sum()anda.mean()return NumPy scalars. Usingint()andfloat()converts them to plain Python types forRESULT, avoiding repr differences across NumPy versions (e.g.np.int64(24)vs24).- For the equivalent pure-Python idiom see
list-sum-meanin the python-data-basics book, andarithmetic-meanin the statistics chapter. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.