⚙️ Setup¶
Same clean style as the chapter notebook.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
NAVY="#0a1230"; INK="#1a2138"; INK_SOFT="#4a5578"
CYAN="#0891b2"; PURPLE="#7c3aed"; AMBER="#d97706"; GREEN="#059669"; PINK="#db2777"; BLUE="#2563eb"
GRID="#e6e9f2"
plt.rcParams.update({
"figure.facecolor":"white","axes.facecolor":"white","figure.dpi":110,"font.size":11,
"axes.edgecolor":GRID,"axes.linewidth":1.2,"axes.grid":True,"grid.color":GRID,"axes.axisbelow":True,
"axes.spines.top":False,"axes.spines.right":False,
"axes.titlesize":14,"axes.titleweight":"bold","axes.titlecolor":INK,
"axes.labelcolor":INK_SOFT,"xtick.color":INK_SOFT,"ytick.color":INK_SOFT,"legend.frameon":False,
})
print("✅ Ready.")
✅ Ready.
[5000, 7000, 8000, 6000, 10000, 9000, 7000]. Find the mean and the range.steps = pd.Series([5000, 7000, 8000, 6000, 10000, 9000, 7000])
mean_steps = steps.mean()
range_steps = steps.max() - steps.min()
print(f"Mean : {mean_steps:.0f} steps/day")
print(f"Range : {range_steps} steps (max {steps.max()} - min {steps.min()})")
Mean : 7429 steps/day Range : 5000 steps (max 10000 - min 5000)
fig, ax = plt.subplots(figsize=(8.5,4.2))
dlabels = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
ax.bar(dlabels, steps, color=CYAN, alpha=0.85, edgecolor="white")
ax.axhline(mean_steps, color=PURPLE, ls="--", lw=2.2)
ax.text(6, mean_steps+150, f"mean = {mean_steps:.0f}", color=PURPLE, fontweight="bold", ha="right")
ax.set_title("Challenge 1 — Daily steps", loc="left")
ax.set_ylabel("Steps")
plt.tight_layout(); plt.show()
Answer: Mean ≈ 7,429 steps/day, Range = 5,000 steps (10,000 − 5,000). This is descriptive statistics, summarizing the data we have.
qs = [
("Is there a relationship between hours slept and test scores?", "EXPLAIN (statistics)"),
("Will this specific customer cancel their subscription next month?", "PREDICT (machine learning)"),
("What was our average daily revenue last quarter?", "EXPLAIN / describe (statistics)"),
]
for q, a in qs:
print(f"• {q}\n -> {a}\n")
• Is there a relationship between hours slept and test scores?
-> EXPLAIN (statistics)
• Will this specific customer cancel their subscription next month?
-> PREDICT (machine learning)
• What was our average daily revenue last quarter?
-> EXPLAIN / describe (statistics)
Answer: Questions about relationships or summaries of existing data are explanation/description (statistics). Questions about what will happen for a new case are prediction (machine learning).
[22, 25, None, 30, 28] (one value is missing). (a) What cleaning step is needed? (b) Find the mean of the known ages.ages = pd.Series([22, 25, None, 30, 28])
print("Raw ages:", list(ages))
print("Missing values:", int(ages.isna().sum()))
# (a) Cleaning step: handle the missing value.
# (b) Mean of the KNOWN ages (pandas .mean() skips missing automatically):
print(f"Mean of known ages: {ages.mean():.1f}")
# A common cleaning choice: fill the gap with that mean
filled = ages.fillna(round(ages.mean(), 1))
print("After filling the gap:", list(filled))
Raw ages: [22.0, 25.0, nan, 30.0, 28.0] Missing values: 1 Mean of known ages: 26.2 After filling the gap: [22.0, 25.0, 26.2, 30.0, 28.0]
Answer: (a) The data must be cleaned, handle the missing value (drop it or fill it, e.g. with the mean). (b) Mean of the four known ages = 26.25. Cleaning before analysis is a core data-science habit.
houses = pd.DataFrame({
"size_sqft": [900, 1200, 1500, 2000],
"bedrooms": [2, 3, 3, 4],
"age_years": [30, 12, 8, 2],
"price_k": [180, 250, 310, 420], # <- this is what we want to predict
})
label = "price_k"
features = [c for c in houses.columns if c != label]
print("FEATURES (inputs the model learns from):", features)
print("LABEL (the target we predict) :", label)
houses
FEATURES (inputs the model learns from): ['size_sqft', 'bedrooms', 'age_years'] LABEL (the target we predict) : price_k
| size_sqft | bedrooms | age_years | price_k | |
|---|---|---|---|---|
| 0 | 900 | 2 | 30 | 180 |
| 1 | 1200 | 3 | 12 | 250 |
| 2 | 1500 | 3 | 8 | 310 |
| 3 | 2000 | 4 | 2 | 420 |
Answer: Features = size_sqft, bedrooms, age_years (the inputs). Label = price_k (the thing we predict). Every supervised-ML problem is "features → label."
tasks = {
"Report the average watch time per user this month": "STATISTICS (summarize data)",
"Predict which show a user will watch next": "MACHINE LEARNING (predict)",
"Build the full recommendation product end-to-end": "DATA SCIENCE (the whole pipeline)",
}
for task, field in tasks.items():
print(f"• {task}\n -> {field}\n")
• Report the average watch time per user this month
-> STATISTICS (summarize data)
• Predict which show a user will watch next
-> MACHINE LEARNING (predict)
• Build the full recommendation product end-to-end
-> DATA SCIENCE (the whole pipeline)
Answer: summarizing existing numbers = Statistics; predicting a new outcome = Machine Learning; combining data, stats, ML, code and domain knowledge into a working product = Data Science. They complement each other, statistics is the foundation, ML adds prediction, data science delivers the whole solution.