Correlation vs Causation
Two variables move together — but does one cause the other?
Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
Correlation measures how strongly two variables move together. The Pearson correlation coefficient r ranges from -1 to +1:
- r = +1 — perfect positive correlation (as X increases, Y increases proportionally)
- r = 0 — no linear relationship
- r = -1 — perfect negative correlation (as X increases, Y decreases)
- r = 0.7 → strong positive, r = 0.3 → weak positive (rough guides)
import pandas as pd
df = pd.DataFrame({'hours_studied': [1,2,3,4,5], 'score': [55,60,65,70,75]})
print(df.corr()) # r = 1.0 (perfect positive)Causation means one variable directly causes the other. Correlation does NOT imply causation.
Classic example: Ice cream sales and drowning rates are positively correlated. Does ice cream cause drowning? No — both are caused by a third variable: hot weather (a confounding variable).
In data science this matters enormously: a model can find a correlation and use it for prediction without that relationship being causal.
Examples
Correlation matrix with pandas
corr() computes pairwise Pearson correlation for all numeric columns
import pandas as pd
df = pd.DataFrame({
'age': [25, 30, 35, 40, 45],
'salary': [40, 55, 65, 72, 80],
'height': [170, 168, 175, 172, 169]
})
print(df.corr().round(2))
# age & salary: high correlation
# height: low correlation with bothHow well did you understand this?
Next in Mathematics for Data Science
Probability Basics