Drag the points and watch the correlation coefficient change. Every point adds one term to the formula: teal when it sits on the same side of both means, burgundy when it sits on opposite sides. The shaded rectangles are those terms, drawn to scale. Start with the dogs and cats data from class.
same side of both means: positive termopposite sides: negative termmeans
Pearson's r—
positive terms (sum)—
negative terms (sum)—
numerator Σ(a−ā)(b−b̄)—
points n—
r = Σ(a−ā)(b−b̄) / √( Σ(a−ā)² · Σ(b−b̄)² )
Drag a point to move it. Click an empty spot to add a point; double-click a point to remove it. Keyboard: tab to a point, move it with the arrow keys (hold shift for bigger steps), remove it with Delete.
The same number as a cosine
Subtract each column's mean. The two centered columns are vectors with one entry per point. Pearson's r is the cosine of the angle between them, the same cosine similarity you used for the movie recommender. Without the centering you would get a different number.
angle θ between the centered columns—
cos θ—
Every term, one row per point
#
a
b
a − ā
b − b̄
term
Reading r
Sign = direction, size = strength. r runs from −1 (all points on a falling line) through 0 to +1 (all points on a rising line).
r measures straight-line association only. A strong curved relationship can still give r close to 0. Try the curve preset, and always plot your data.
Two features with r = −0.85 are largely redundant, just as with +0.85: knowing one tells you a lot about the other. The sign only says which way they move.
For prediction, look at r between each feature and the target, for example every column against median_house_value with corr_matrix["median_house_value"].
Built to accompany COSC 3430 (Artificial Intelligence), end-to-end ML project, part 4 (correlations). Everything here computes live in the browser from the actual formula; check it against df.corr() in your notebook.