Chapter 7 · Solutions
Practice Challenges, Worked Answers ✅
Full solutions to the five "Math Refresher" challenges, one per math area. Try them yourself first, then compare.
Statistics, Data Science and AI: A Visual Handbook · John Fisher · 2026
⚙️ Setup¶
In [1]:
import numpy as np
import math
import pandas as pd
print("✅ Ready.")
✅ Ready.
CHALLENGE 1 · SUMMATION
Compute Σ by hand and in code
For x = [3, 7, 2, 8]: compute Σxᵢ, then the mean (1/n)·Σxᵢ. Verify against NumPy.
In [2]:
x = np.array([3, 7, 2, 8])
total = x.sum()
mean = total / len(x)
print(f"Σ xᵢ = {total}")
print(f"mean = {mean} | np.mean = {np.mean(x)}")
Σ xᵢ = 20 mean = 5.0 | np.mean = 5.0
Answer: Σxᵢ = 20, mean = 20/4 = 5.0. Σ is just "add them all"; dividing by n gives the mean.
CHALLENGE 2 · SETS
Union and intersection
Let A = {1,2,3,4} and B = {3,4,5,6}. Find A∪B (or), A∩B (and), and how many elements are in the union.
In [3]:
A = {1, 2, 3, 4}; B = {3, 4, 5, 6}
print("A ∪ B =", A | B)
print("A ∩ B =", A & B)
print("size of union =", len(A | B))
A ∪ B = {1, 2, 3, 4, 5, 6}
A ∩ B = {3, 4}
size of union = 6
Answer: A∪B = {1,2,3,4,5,6} (6 elements), A∩B = {3,4}. In probability, ∪ is "or" and ∩ is "and".
CHALLENGE 3 · COUNTING
Permutation or combination?
(a) How many ways can 4 runners finish 1st/2nd/3rd? (b) How many 3-person teams can you form from 4 people?
In [4]:
# (a) order matters (podium positions) -> permutation P(4,3)
print("(a) finishing orders P(4,3) =", math.perm(4,3))
# (b) order does NOT matter (a team) -> combination C(4,3)
print("(b) possible teams C(4,3) =", math.comb(4,3))
(a) finishing orders P(4,3) = 24 (b) possible teams C(4,3) = 4
Answer: (a) 24 ordered finishes (order matters → permutation). (b) 4 teams (order doesn't matter → combination).
CHALLENGE 4 · LOGARITHMS
Products into sums
Show that log(8 × 32) equals log(8) + log(32). Then: what is log₂(1024)?
In [5]:
print("log(8·32) =", round(np.log(8*32), 4))
print("log(8) + log(32) =", round(np.log(8) + np.log(32), 4))
print("\nlog₂(1024) =", int(np.log2(1024)), " (because 2¹⁰ = 1024)")
log(8·32) = 5.5452 log(8) + log(32) = 5.5452 log₂(1024) = 10 (because 2¹⁰ = 1024)
Answer: Both equal ≈ 5.545, logs turn multiplication into addition. And log₂(1024) = 10, since 2¹⁰ = 1024.
CHALLENGE 5 · SLOPES & MATRICES
Derivative and a matrix mean
(a) The loss is f(x) = (x−5)². Its derivative is f'(x) = 2(x−5). What is the slope at x=2, and which way is downhill? (b) For the matrix X below, compute the mean of each column.
In [6]:
# (a) derivative / slope
slope_at_2 = 2*(2 - 5)
print(f"(a) f'(2) = {slope_at_2} -> slope is negative, so downhill is to the RIGHT (toward x=5).")
# (b) column means of a data matrix
X = np.array([[2, 10],
[4, 20],
[6, 30]])
print("(b) column means:", X.mean(axis=0))
(a) f'(2) = -6 -> slope is negative, so downhill is to the RIGHT (toward x=5). (b) column means: [ 4. 20.]
Answer: (a) f'(2) = −6; a negative slope means the function is falling, so the minimum (x=5) is to the right, gradient descent would step that way. (b) Column means = [4, 20]. Linear algebra averages the whole column at once.
🎉 Nicely done!
You worked through summation, sets, counting, logarithms, slopes, and matrices, the exact math the rest of the book leans on. With Part I complete, you're ready to start describing data.
Statistics, Data Science and AI: A Visual Handbook · © 2026 John Fisher