/W02
实验Lab: Penguins by the numbers
- Dataset
- Palmer penguins (344 rows, CC0)
- Time
- 2 to 3 hours
- Before you start
- slides ch 3 quiz ch 3 notebook (opens in a new tab)
This week you'll be able to
- Compute and interpret mean, variance, and correlation, and say when correlation misleads.
- Apply Bayes' rule to a base-rate problem and explain the result in words.
- Express a linear model as matrix operations and solve least squares with NumPy.
Goal
Use descriptive statistics, Bayes' rule, and matrix algebra on a small real dataset. Check each result against what the chapter says it should show.
Data
Measurements of three penguin species from Palmer Station, Antarctica. Published by Horst, Hill, and Gorman under CC0.
import pandas as pd
penguins = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv")
penguins = penguins.dropna() # 11 rows have gaps. Week 5 deals with missing data properly.
penguins.head()Tasks
- Compute the mean, median, and standard deviation of body mass for each species (Procedure 3.5). Which species is heaviest? Which varies most?
- Compute the correlation between bill length and bill depth for all penguins. Then compute it within each species (Procedure 3.1). Explain why the sign changes.
- Test whether flipper length is normal within one species (Procedure 3.2). Report the p-value and say what it does and does not tell you (Procedure 3.4).
- Bayes' rule: a penguin has a flipper over 205 mm. Estimate P(Gentoo | flipper > 205 mm) from the data in two ways. First count directly. Then use Bayes' rule with P(flipper > 205 mm | Gentoo), P(Gentoo) and P(flipper > 205 mm). Check that the two agree.
- Build the design matrix X for body mass from flipper length and bill length, with a column of ones. Solve the normal equations with NumPy (Procedure 3.3). Compare with
np.linalg.lstsqand with scikit-learn'sLinearRegression.
Deliverable
A notebook with the five answers. Each numeric answer gets one sentence saying what it means.
Done when
- Task 2 shows the overall and per-species correlations side by side, with an explanation.
- Task 4's two answers match to three decimal places.
- Task 5's three methods give the same coefficients.
Stretch
Explain why np.linalg.lstsq is preferred over inverting in practice. Build a case where the inverse fails: add a column equal to twice another.