pearson相关系数python代码
时间: 2023-08-15 14:09:06 浏览: 222
Sure! Here's a simple Python code snippet to calculate the Pearson correlation coefficient using the `numpy` library:
```python
import numpy as np
# Example data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
# Calculate Pearson correlation coefficient
corr_matrix = np.corrcoef(x, y)
corr_coeff = corr_matrix[0, 1]
print("Pearson correlation coefficient:", corr_coeff)
```
In this example, we have two lists `x` and `y` representing the variables for which we want to calculate the correlation coefficient. We use the `np.corrcoef()` function from the `numpy` library to compute the correlation matrix, and then extract the correlation coefficient from it. Finally, we print the result.
You can replace the `x` and `y` lists with your own data to calculate the Pearson correlation coefficient for your specific case.
阅读全文