Worked examples — Exploratory data analysis (EDA) workflow
First, earn every symbol
Before a single example, let us make sure nothing is assumed. The parent note wrote this formula:
Now let us slowly read the formula out loud, symbol by symbol.
Covariance — the honest name for the top of the fraction
and use the SAME — so it cancels
Now watch the vanish. Rewrite using covariance and the two 's:
Why can never exceed 1 — the geometric reason
The "unitless shape" story is nice, but why is trapped in ? Here is the visual argument.
This is precisely the Cauchy–Schwarz inequality, , which says the dot product can never beat the product of the lengths — geometrically, a projection is never longer than the thing you project. Figure s01 shows the two arrows and the angle.
Read figure s01 (alt-text: two vectors and their angle). On the left panel, the raw data: five blue dots, a red dashed mean-cross at splitting the plane into four quadrants, and a small green arrow from the cross to each dot — every arrow lands in the "agree" quadrants (both-above or both-below), the visual meaning of near . On the right panel, the same data re-drawn as the two big arrows (x-deviations, blue) and (y-deviations, yellow) with the angle between them marked in red; because is small, . The narrow angle is the whole reason is nearly 1.
Everything below uses only these ideas. Prerequisites live in 2.1.15-Statistical-measuresfor-data-analysis (mean, std, median) and 2.1.16-Data-visualization-techniques (scatterplots, boxplots).
The scenario matrix
Every correlation / univariate situation falls into one of these cells. The examples that follow are labelled with the cell they cover, and together they hit every row.
| # | Case class | What's special | Example |
|---|---|---|---|
| A | Strong positive () | points hug a rising line | Ex 1 |
| B | Strong negative () | points hug a falling line | Ex 2 |
| C | No linear relation () | shapeless blob | Ex 3 |
| D | Curved but (degenerate meaning) | perfect pattern, zero | Ex 4 |
| E | Zero-spread column () & tiny- | denominator , undefined; edge case | Ex 5 |
| F | Outlier hijack | one point flips the number | Ex 6 |
| G | Skew: mean vs median (univariate) | which way the tail points | Ex 7 |
| H | Categorical vs numerical (grouped stats) | word problem / business decision | Ex 8 |
| I | Exam twist: correlation ≠ causation & multicollinearity | interpretation trap | Ex 9 |
| J | Moderate + sample size / significance | is real or luck? | Ex 10 |
Example 1 — Cell A: strong positive correlation
Example 2 — Cell B: strong negative correlation
Read figure s02 (alt-text: downward best-fit line hugged by points). The red line is the best-fitting straight line through the five blue dots; it slopes downward. Every dot hugs that line closely — the visual signature of near 1. Compare it mentally with figure s01: same tightness, opposite tilt. Tilt direction = sign of ; tightness of the hug = magnitude of .
Example 3 — Cell C: no linear relationship
Example 4 — Cell D: perfect pattern, but (a trap!)
Read figure s03 (alt-text: dots on a symmetric parabola with mean line). The green curve is the exact parabola ; the blue dots sit perfectly on it — a flawless relationship. Yet the red dashed horizontal line marks , and you can see the dots are arranged symmetrically left-and-right about the vertical mean line: for every dot that pulls the product positive on the right, a mirror dot cancels it on the left. That left-right symmetry is why the votes sum to zero.
Example 5 — Cell E: a constant column () and the edge case
| row | age | country_code |
|---|---|---|
| 1 | 20 | 91 |
| 2 | 35 | 91 |
| 3 | 50 | 91 |
Compute between age and country_code.
Forecast: never changes. What can "moving together" even mean? Guess: undefined.
Step 1 — mean of . .
Step 2 — y-deviations. for every row. So every product → numerator .
Step 3 — compute BOTH denominator sums. The side is healthy: , so The side collapses: , so . This is . Why this step matters: we must write out both pieces to see that the denominator is — we are about to divide by zero.
Step 4 — assemble.
Verify: pandas returns NaN here, confirming "undefined". Sanity: a column with zero spread carries no information, so no correlation could exist.
is NOT "zero correlation". has a definite meaning — "arrows perpendicular, no linear tendency". But when the y-arrow has zero length: there is no direction to compare against, so has no value at all (you cannot measure the angle to a zero-length arrow). Assigning would falsely claim "we measured no relationship" when in truth we could not measure anything. That difference matters: a constant column should be flagged and dropped, not silently treated as "uncorrelated".
Near-constant columns (std tiny but nonzero) are just as dangerous: becomes numerically unstable and hypersensitive to one wobble. Robust practice: drop columns where df[col].nunique() == 1, and for near-constant ones prefer robust spread measures (IQR) or simply remove them — see 2.2.8-Feature-selection-methods.
degenerate case — is always . Correlation needs distinct points to mean anything, and exactly two points always give (unless a column is constant). Why? Two points determine one straight line perfectly — there is no scatter to disagree with it, so the arrows are exactly parallel or anti-parallel. Concretely, take , : , numerator , denominator , so regardless of the actual slope. Lesson: never trust a correlation from 2 rows — it is guaranteed and tells you nothing (this is the extreme of Ex 10's sample-size warning).
Example 6 — Cell F: one outlier hijacks the number
Clean data: and — a perfect line, . Now a data-entry error adds the point (a decimal-point typo). Recompute .
Forecast: four out of five points are perfect. Guess stays near ? Watch what happens.
Step 1 — means with the outlier. , . Why? The single value dragged from a healthy down to — the mean is not robust.
Step 2 — products.
| product | ||
|---|---|---|
Step 3 — numerator. .
Step 4 — denominator. . .
Verify: — the sign flipped from to negative because of one bad row! The recomputed value is shown, and figure s04 pictures exactly which point did the damage.
before trusting . A single extreme point can invert a correlation (here ). See 2.1.5-Outlier-detection-and-treatment. This is why we plot the scatter (figure s04): the eye instantly spots the lone renegade point.
Read figure s04 (alt-text: four collinear dots plus one far-off red dot). Four blue dots line up on a perfect diagonal — that cluster alone would give . The red dot at the bottom-right is the typo ; the red arrow labels it "one bad row flips r to −0.67". Because that one point sits so far below everything, it single-handedly drags the mean of down and reverses the sum of products. The lesson is visual: one point far from the crowd can outvote all the others.
Example 7 — Cell G: mean vs median tells you the skew direction
Incomes (k$): .
Forecast: one huge income of 300. Guess the mean is pulled above the median → right-skew.
Step 1 — median. Eight numbers sorted; median = average of the 4th and 5th . Why? The median is the middle — it ignores how big the extreme is, only its position.
Step 2 — mean. . Why? The mean does feel the size of 300, so it gets yanked upward.
Step 3 — compare. Mean median . Why this step? The parent note's rule: mean > median ⇒ positive (right) skew, a long tail to the high side.
Verify: difference of k confirms a strong right tail. A histogram would show a lump at low incomes and a lonely bar far to the right.
The mean gets dragged toward the long tail; the median stays put. So whichever side the mean is on relative to the median, that is where the tail lives. This flags the column for a log transform.
Example 8 — Cell H: categorical vs numerical (business word problem)
A shop records, for each product category, the average purchase amount, the middle purchase amount, and the standard deviation (std = spread within that category):
| category | mean $ | median $ | std $ () |
|---|---|---|---|
| Electronics | 250 | 220 | 80 |
| Clothing | 120 | 110 | 45 |
| Books | 45 | 40 | 15 |
| Food | 30 | 28 | 10 |
Question: is product_category worth keeping to predict purchase amount? Quantify how much the category "explains".
Forecast: Electronics is ~8× Food. Guess: category matters a lot.
Step 1 — spread between groups. The group means are ; their average is . How far do group means sit from this grand mean? Deviations: . Squared and averaged: Why this step? If knowing the category shifts the expected purchase a lot, the group means sit far apart from the grand mean — that far-apartness is the signal category provides. This is the same "spread from a centre" idea as , but applied to the group means instead of raw points.
Step 2 — spread within groups. Average the squared stds (each std is a group's internal spread): . Why this step? This is the leftover noise inside each category — the wobble that category cannot explain, because two Electronics buyers still differ from each other.
Step 3 — the ratio (a signal-to-noise idea). Why this step? This "correlation-ratio-squared" divides signal by (signal + noise). It says ≈78% of the variation in purchase amount is explained by knowing the category.
Step 4 — decide. is close to , so category explains the lion's share of purchase-amount variation. Why this step? EDA's whole point is to guide feature choices; a ratio this high is a green light.
Verify: ✓ and near , confirming the forecast — keep product_category. This grouped-statistics reasoning is exactly the boxplot/ANOVA logic in 2.1.16-Data-visualization-techniques and connects to 2.2.8-Feature-selection-methods.
Example 9 — Cell I: exam twist — high that lies
). Should ice-cream be a model feature? A city finds monthly ice-cream sales and drowning counts have .
Forecast: is huge — surely ice-cream causes drownings? Say it out loud, then read on.
Step 1 — trust the number's shape claim only. correctly says: months of high ice-cream sales tend to be months of many drownings. The arithmetic is honest — the dots really do hug a rising line. Why this step? only reports co-movement, nothing about mechanism; we must not read more into it than it measures.
Step 2 — hunt for a hidden third variable. Both rise in summer. Heat drives both ice-cream buying and swimming (hence drownings). Temperature is a confounder — a lurking cause of both columns. Why this step? Correlation is symmetric and blind to causes; a shared driver can manufacture a big with no direct link between the two columns.
Step 3 — the modelling consequence. If your dataset also has temperature, then ice_cream_sales becomes a redundant near-duplicate of it — this is multicollinearity, two features carrying the same information.
Why this step? Redundant predictors confuse a linear model about which one deserves credit → unstable, wildly swinging coefficients → worse generalisation. See 3.1.1-Bias-variance-tradeoff.
Step 4 — decide. Report: "strong correlation, likely confounded by temperature; do NOT claim causation; drop or combine the collinear feature." Why this step? An EDA finding must translate into an action, not just a number.
Verify (sanity, not arithmetic): the two takeaways are internally consistent — a confounded high is exactly the situation where two predictors overlap, so "beware causation" and "beware multicollinearity" are the same warning seen from two sides.
(1) Correlation ≠ causation — a big needs a mechanism before you claim cause. (2) High between two predictors (multicollinearity) is a problem, not a bonus — drop or combine them (2.2.8-Feature-selection-methods).
Example 10 — Cell J: a moderate — real signal or lucky noise?
from only 5 rows vs from 1000 rows Two analysts both report . Amina computed it from 5 customers; Ben from 1000 customers. Should you trust them equally?
Forecast: same number, so... same confidence? Think again — one has almost no evidence behind it.
Step 1 — what "moderate" even means. An near is a moderate linear link: the dots lean along a line but scatter widely around it (unlike Ex 1's tight ). Roughly, of the variation is explained — three-quarters is still noise. Why this step? Interpreting magnitude is as important as sign; is neither "nothing" (Ex 3) nor "strong" (Ex 1).
Step 2 — bring in sample size . With only points, even unrelated columns often produce around by pure chance — five random dots can fall into a leaning shape easily (and at it is always , as Ex 5 showed). With , random noise averages out and a spurious is astronomically unlikely. Why this step? alone hides how much evidence stands behind it; the same number means different things at different .
Step 3 — the -value idea (plain words). A -value answers: "if the two columns were truly unrelated, what is the chance of seeing an this big just by luck?" Small (say ) = "luck alone rarely does this, so the link is probably real". For : at , (huge — could easily be luck); at , is essentially (real). Why this step? The -value converts "how many rows" into "how much can I trust this ".
Step 4 — decide. Trust Ben (1000 rows, tiny ); treat Amina's as inconclusive — gather more data before acting. Why this step? EDA must separate a reliable moderate signal from a fragile one.
Verify: the numbers are internally consistent — larger shrinks the -value for the same . Quick rule of thumb often taught: is "significant" at 95% confidence when . At : threshold → not significant. At : threshold → significant. Both match Steps 3–4. ✓
without . A correlation from a handful of rows is almost meaningless. Always ask "how many points?" and, ideally, check the -value before believing a moderate .
Recall
Recall In one sentence, what is
? A single number in measuring how tightly two columns rise-and-fall together in a straight-line way: rising line, falling line, no linear link. Geometrically it is between the two deviation-arrows.
Recall Where does the
go — population vs sample? Both top () and bottom (, each carrying ) contain one factor of ; they cancel. So is unaffected by whether you divide by or not — the constant is irrelevant as long as top and bottom match.
Recall Why can
never exceed 1? Because and Cauchy–Schwarz says — a projection is never longer than the vector itself.
Recall What is covariance and how does it relate to
? — average co-movement in raw units. Then : covariance divided by the two spreads to make it unit-free.
Recall
for . Does that mean no relationship? (Ex 4) No — it means no linear relationship. Pearson is blind to curves; always plot the scatter.
Recall What happens when a column is constant, and why isn't that
? (Ex 5)
Its std is , so has zero length and there is no angle to take a cosine of → is undefined (NaN), not . "" would falsely claim "measured, no link"; the truth is "could not measure". Drop zero-variance columns.
Recall Why is
always when ? (Ex 5) Two points determine one line perfectly — no scatter to disagree, so the deviation-arrows are exactly parallel/anti-parallel. Never trust a