Calculate the Pearson correlation coefficient between two features in a dataset.
The Pearson correlation coefficient measures the strength and direction of a linear relationship between two variables: r = cov(X,Y) / (std_x × std_y), always in the range [-1, 1]. r = 1 means a perfect positive linear relationship, r = -1 means a perfect negative linear relationship, and r = 0 means no linear relationship. In ML, correlation is widely used for feature selection (dropping one of a pair of highly correlated features to reduce redundancy/multicollinearity) and for exploratory data analysis.
r = cov(X, Y) / (std_x × std_y)
R² (r squared) represents the proportion of variance in one variable that is explained by a linear relationship with the other — e.g. r = 0.8 gives R² = 0.64, meaning 64% of the variance is 'explained' by the linear relationship.
Pearson correlation only captures linear relationships — two variables can have a strong nonlinear relationship (e.g. quadratic) and still show low or zero Pearson correlation, so it should be complemented with visualization or nonlinear correlation measures like Spearman's rank correlation.
Highly correlated feature pairs (e.g. |r| > 0.9) often carry redundant information; removing one of the pair can reduce multicollinearity in linear models and slightly simplify the model without losing much predictive power.
No — a strong correlation between two variables does not establish that one causes the other; both could be driven by a third confounding variable, or the relationship could be coincidental in a small sample.