Statistics · Module 1Module material
Descriptive statistics cheat sheet
How to describe a dataset and read its plots: what to fix before any statistic, which view answers which question, the formulas, the code and the gotchas. Free to read and share, no account needed.
Before any plot or statistic
- Name the unit of analysis: the thing counted once. One trip, one gross invoice (not one invoice line), one person, one client record.
- Declare the rows and rules first: filters, bin edges, bands, groups and thresholds are fixed before you read any result.
- Keep the count beside every shape:
nfor each group, and the numerator and denominator beside every rate. - A display sample is for marks only: every count, quantile and coefficient uses all the declared rows.
Which view answers which question
| question | use | read it as |
|---|---|---|
| How many observations fall in each numeric interval? | count histogram | bar height = count; state the bin width |
| What share falls in an interval of any width? | density histogram | bar area = fraction; height is per unit (per minute) |
| What smoothed shape do the values have? | KDE, with a rug | a smoothed estimate; its height is not a fraction |
| What fraction is at or below a value? | empirical CDF | read up from the value, across to the fraction |
| What value first reaches a fraction? | ECDF first crossing, quantile | read across from the fraction, down to the value |
| Where is the middle? | median | resists one extreme value |
| What is the arithmetic average? | mean | uses every value; pulled by a long tail |
| Which exact value repeats most? | mode (stated precision) | may describe one repeated amount, not the bulk |
| How spread out is the whole group? | sample SD | in the original unit; sensitive to extreme values |
| How wide is the middle half? | IQR | resists the tail |
| How do several groups compare? | box plots, violin plots, on one shared axis | quartiles and flags from rows; violin outline is estimated |
| Do two numeric values move together in a straight line? | scatter plot, then Pearson r | one coefficient, not every pattern |
| How often does an outcome occur in each category? | contingency table, then rates | rate = group events / group total |
| Where are paired values concentrated? | hexbin joint plot with marginals | joint shows pairs; marginals show one variable each |
| Which of many pairs deserves a closer look? | pair plot and correlation heatmap on training rows | a candidate for a later check, not a proven feature |
| Who is in the rows at all? | row shares beside published control totals | a row share describes rows, not the target population |
Formulas, as the videos build them
| quantity | formula | Module 1 example |
|---|---|---|
| density of a bin | count / (n × bin width) | 981 / (3,818 × 5) ≈ 0.0514 per minute; area 0.0514 × 5 ≈ 25.7% |
ECDF at x | (observations <= x) / n | at 20 min: 2,937 / 3,818 = 76.9% |
first-crossing rank for fraction q | ceil(q × n), then read that sorted value | 0.9 × 3,818 = 3,436.2 → rank 3,437 → 27.1833 min |
| mean | sum / n | gross invoices: £534.40 |
| median | middle sorted value, or mean of the two middle values | (£303.83 + £303.84) / 2 → £303.84 |
| deviation | value − group mean | the signed deviations sum to zero |
| sample variance | sum of squared deviations / (n − 1) | UK: 3,174,016.18 pounds squared |
| sample SD | √(sample variance) | UK £1,781.58, Germany £608.92 |
| IQR | Q3 − Q1 (NumPy linear interpolation) | UK £319.56, Germany £430.41 |
| Tukey fences | Q1 − 1.5 × IQR and Q3 + 1.5 × IQR | Midtown 09: -8.025 and 35.508 min |
| whisker end | most extreme recorded value inside the fences | 0.05 and 35.5 min; 121 flagged |
| covariance | mean product of paired deviations | age and hours: 11.58 |
Pearson r | covariance / (SD of x × SD of y), between −1 and 1 | age and hours: 0.069 on 32,561 rows |
| rate | events in the group / records in the group | graduate school 2,036 / 10,585 = 19.2% |
The calls
edges5 = np.arange(0, 85, 5) # declare edges first
sns.histplot(data=trips, x="duration_min", bins=edges5, stat="count")
sns.histplot(data=trips, x="duration_min", bins=edges5, stat="density")
sns.kdeplot(data=trips, x="duration_min", bw_adjust=1)
sns.rugplot(data=rug_sample, x="duration_min") # display sample only
sns.ecdfplot(data=trips, x="duration_min")
trips["duration_min"].le(20).mean() # fraction at or below 20
np.quantile(trips["duration_min"], 0.9) # value at a fraction
invoices = lines.groupby("InvoiceNo")["line_value"].sum() # lines -> invoices
invoices.mean(), invoices.median(), invoices.round(2).value_counts()
by_country.var(ddof=1), by_country.std(ddof=1) # n - 1 denominator
np.quantile(values, [0.25, 0.75])
sns.boxplot(data=groups, x="group", y="duration_min") # whis=1.5 default
sns.violinplot(data=groups, x="group", y="duration_min", inner="quart")
rows[["age", "hours_per_week"]].corr(method="pearson")
pd.cut(rows["hours_per_week"], [0, 30, 40, 50, 100], include_lowest=True)
pd.crosstab(clients["education"], clients["default"])
sns.countplot(...) # counts records, not rates
sns.barplot(data=rates, x="education", y="rate", errorbar=None) # precomputed rates
sns.scatterplot(data=sample, x="trip_distance", y="fare_amount", alpha=0.2)
sns.jointplot(data=sample, x="trip_distance", y="fare_amount", kind="hex")
sns.pairplot(train_sample[columns]) # display sample of training rows
sns.heatmap(train[columns].corr(), vmin=-1, vmax=1, center=0, annot=True)
pd.read_sas("DEMO_J.xpt", format="xport")
Gotchas
[20, 25)includes20and excludes25:3trips of exactly20minutes sit in that bar, so bars cannot decide a<= 20rule. Count the rows.- A density height is not a percentage; only width × height is.
- An ECDF's vertical axis is always a fraction, never a duration.
- Rounding a percentile down can break the target:
27min covers89.8%,28min covers91.1%. - Average invoice totals, not lines: the mean line value is
£20.12. - The sample variance's unit is pounds squared; report the SD in pounds.
- A point beyond a whisker is a flag for inspection, not a proven error; a
value inside the fences (a
3second trip) is not proven valid either. - Violin widths are scaled per group: they do not show group size.
- Correlation is not causation; subgroups can reverse it (
-0.052inHS-grad,0.203inSome-college). - A high
r(0.939) does not mean one rule fits every row: the$70code-two band. - A random train/test split partitions the rows you already have; it does not change who was selected or who responded.
- The largest count is not the highest rate: university has the most defaults, high school the highest rate.
- Leave
fnlwgtand the income label out of a feature scan, and compute the heatmap on training rows only.