[파이썬] scipy에서 상관 계수

import numpy as np from scipy.stats import pearsonr

Dummy data for two variables

x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 6, 8, 10])

Calculating the Pearson correlation coefficient

corr_coeff, _ = pearsonr(x, y)

print(f”Pearson correlation coefficient: {corr_coeff}”) ```

Scipy is a powerful library in Python for scientific computing and data analysis. It provides various statistical functions, including calculating the correlation coefficient. In this blog post, we will focus on computing the correlation coefficient using Scipy’s pearsonr function.

To start, we need to import numpy to generate some dummy data for two variables x and y. Let’s assume x represents the independent variable, and y represents the dependent variable.

Next, we can use the pearsonr function from the scipy.stats module to calculate the Pearson correlation coefficient between x and y. The pearsonr function returns two values: the correlation coefficient and the p-value. Since we are only interested in the coefficient, we can use _ to discard the p-value.

Finally, we print the Pearson correlation coefficient to the console using an f-string for improved readability.

By running this code, you will get the Pearson correlation coefficient between x and y. This coefficient ranges from -1 to 1, where -1 indicates a perfect negative linear relationship, 0 indicates no linear relationship, and 1 indicates a perfect positive linear relationship.

Using Scipy’s pearsonr function makes it easy to compute the correlation coefficient in Python, allowing for efficient data analysis and decision-making based on the relationship between variables.