Calculating Pearson Correlation in Practice
What you'll learn: How to compute Pearson's correlation coefficient using Python and R with real data.
The Core Idea
Now that you understand what Pearson correlation measures and its assumptions, it's time to calculate it with actual tools. While you could compute it by hand using the formula, data scientists use libraries that handle the math efficiently and accurately. You'll typically work with columns of data in a DataFrame, and the calculation becomes a one-line operation.
Step-by-Step Computation Example
Let's say you have two variables: hours studied and exam scores for 5 students.
Python with Pandas:
import pandas as pd
data = pd.DataFrame({
'hours': [2, 3, 5, 7, 8],
'scores': [65, 70, 80, 85, 90]
})
r = data['hours'].corr(data['scores'])
print(r) # Output: ~0.988
Python with NumPy:
import numpy as np
hours = [2, 3, 5, 7, 8]
scores = [65, 70, 80, 85, 90]
r = np.corrcoef(hours, scores)[0, 1]
print(r) # Output: ~0.988
Python with SciPy:
from scipy.stats import pearsonr
r, p_value = pearsonr(hours, scores)
print(f"r = {r}, p-value = {p_value}")
R:
hours <- c(2, 3, 5, 7, 8)
scores <- c(65, 70, 80, 85, 90)
r <- cor(hours, scores)
print(r) # Output: ~0.988
Notice SciPy also returns a p-value for hypothesis testing—handy for determining if the correlation is statistically significant!
Key Takeaway: Computing Pearson correlation in practice is straightforward using corr() in Pandas, corrcoef() in NumPy, pearsonr() in SciPy, or cor() in R—choose the tool that fits your workflow.