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.
Essential Gigs
For only $50, Nilimesh will develop time series forecasting model for you using python or r. | Note: please contact me…www.fiverr.com
For only $50, Nilimesh will do your data analytics and econometrics projects in python. | Note: please contact me…www.fiverr.com
For only $50, Nilimesh will do your machine learning and data science projects in python. | Note: please contact me…www.fiverr.com
For only $50, Nilimesh will do your gis and spatial programming projects in python. | Note: please contact me before…www.fiverr.com