Read a correlation coefficient as an association, not a cause.
What It Does and Why It Matters
Pearson correlation measures the strength and direction of a linear association between paired measurements; values range from -1 to 1 when both variables vary. A value near zero does not rule out a curved relationship. Outliers can strongly affect Pearson correlation, and correlation alone never establishes causation.
Verify pairs are matched observations. Constant-valued inputs do not yield a meaningful Pearson correlation.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
import statistics
hours = [1, 2, 3, 4, 5]
scores = [52, 61, 68, 81, 90]
print(round(statistics.correlation(hours, scores), 3))
print(statistics.correlation(hours, list(reversed(scores))))
How the Code Works
- The imports make these standard-library tools available: statistics.
- The name hours receives the value of [1, 2, 3, 4, 5]. Changing this input lets you explore a different case.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
Expected output:
0.996
-0.9959020640718428
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Try paired datasets with negative association and no clear linear association.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Claiming that one variable caused the other simply because their correlation is high.
- Overlooking the documented condition: a value near zero does not rule out a curved relationship.
Key Takeaways
- Read a correlation coefficient as an association, not a cause.
- Pearson correlation measures the strength and direction of a linear association between paired measurements; values range from -1 to 1 when both variables vary.
- Try paired datasets with negative association and no clear linear association.