Calculating Pearson correlation and significance in Python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The Pearson correlation coefficient is a statistical measure that captures the linear relationship between two variables. It quantifies how well the change in one variable predicts the change in another. A Pearson correlation of +1 indicates a perfect positive linear relationship, 0 indicates no linear relationship, and -1 implies a perfect negative linear relationship. In this article, we will explore how to calculate the Pearson correlation coefficient and assess its significance in Python.
Why Pearson Correlation?
Before delving into the calculation, it's important to understand why Pearson correlation is widely used:
- Linear Association: It measures the strength and direction of the linear relationship between variables.
- Standardization: The values of the coefficient are standardized between -1 and 1, which gives a simple interpretation.
- Ease of Computation: Calculation is straightforward and computationally efficient.
Calculating Pearson Correlation in Python
Necessary Libraries
To calculate the Pearson correlation coefficient and determine its significance, we will need the following libraries in Python:
numpy: This is used to handle numerical operations.pandas: Useful for managing datasets.scipy: Provides statistical functions, including correlation calculations.- Pearson Correlation Coefficient: This value indicates the direction and strength of the linear relationship.
- P-value: This tells us about the statistical significance of the correlation. A small p-value (typically ≤ 0.05) indicates strong evidence against the null hypothesis, thus, the correlation is significant.
- Normality: Pearson correlation assumes that both variables are normally distributed. If this is not the case, Spearman rank correlation could be considered as an alternative.
- Linearity: It measures only the linear relationship, and relationships that are non-linear are not well-captured by Pearson correlation.
- Outliers: The result is greatly affected by outliers, which may artificially inflate or deflate the correlation coefficient.
- **
numpy.corrcoef**: Use this method if you are interested in the correlation matrix of multiple variables. - Visualization: Always visualize your data;
matplotlibandseabornoffer functions likescatterplotorheatmapfor better insights. - Assumptions: Always verify that the assumptions for Pearson correlation (like normality, homoscedasticity) are met.
Related reading
- Calculating Percentiles on the fly
- Calculating the contours of a 3D heightmap?
- Calculating the number of true positives from a precision-recall curve
- Calculating the relevance of a User based on Specific data
- Calculating powa,b mod n
- Calculating Standard Deviation of Angles?
- Calculating Precision, Recall and F-score in one pass - python
- Calculating the similarity of two lists

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.