Hits: 281
How to determine Pearson’s correlation in Python
Pearson’s correlation is a statistical method that is used to measure the strength of a linear relationship between two variables. It ranges from -1 to 1, where -1 indicates a strong negative correlation, 0 indicates no correlation, and 1 indicates a strong positive correlation. In Python, we can calculate Pearson’s correlation coefficient using the scipy
library.
from scipy.stats import pearsonr
// Create some sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
// Calculate the Pearson's correlation coefficient
corr, p_value = pearsonr(x, y)
print("Pearson's correlation coefficient:", corr)
This will output:
Pearson's correlation coefficient: 0.9966
Here, pearsonr()
function takes in two arrays as input: x and y and returns two values: correlation coefficient and p-value. Correlation coefficient will be between -1 and 1 and p-value will be a probability score which helps to determine the significance of the correlation.
In this example, we get a correlation coefficient of 0.9966 which is quite close to 1 and indicates a strong positive correlation between x and y.
It’s important to note that, while correlation indicates the strength of a relationship between two variables, it does not indicate causality. It’s also important to check for outliers and missing values before calculating correlation coefficients.
In simple words, Pearson’s correlation coefficient is a statistical method that helps to measure the strength of the linear relationship between two variables. In python, we can calculate Pearson’s correlation coefficient using the scipy library’s pearsonr()
function. It will give correlation coefficient and p-value as output, which helps to determine the significance of correlation between two variables.
In this Learn through Codes example, you will learn: How to determine Pearson’s correlation in Python.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.