Menu
CoddyTech

Correlation Is Not Cause

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 33 of 64.

r only sees straight lines. Here y is fully decided by x, yet r is 0:

A scatter plot of y = x squared for x from -3 to 3: a perfect U of points, titled r = 0.0

The falling half cancels the rising half. An r near 0 means no straight line, not no relationship: plot before you drop a feature.

And one far point can fake a strong r, so check outliers first:

Eight blue points in a loose blob near 4 across and 5 up with r = -0.27, and one red point far away at 20 and 25. With it, r jumps to 0.95

Finally, r never proves a cause. Older children have bigger shoes and read better: age, a hidden third variable, drives both.

challenge icon

Challenge

Medium

A tea shop tests its cups: steep and taste for 9 cups (steeping minutes, score), sugar and score for 10 others (sugar cubes, score). The program reads one extra cup's sugar, then score.

  1. Print the r of steep and taste.
  2. Print the r of sugar and score, then with the extra cup added.

Round to 2 decimals, labelled as below. For another shop and extra cup 10, 2.0:

Steeping r: -0.05
Sugar r: -0.23
Sugar r with the extra cup: -0.88

Try it yourself

import numpy as np

# Steeping time in minutes and taste score of 9 cups
steep = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
taste = np.array([3.1, 5.0, 6.6, 7.7, 8.1, 7.6, 6.5, 5.1, 3.4])

# Sugar cubes and taste score of 10 other cups
sugar = np.array([0, 2, 1, 3, 0, 2, 1, 3, 2, 1])
score = np.array([6.5, 5.8, 7.4, 6.1, 5.9, 7.0, 6.2, 6.8, 6.4, 5.7])

# The extra cup
extra_sugar = float(input())
extra_score = float(input())

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler