python 相关系数 气象_三大相关系数: pearson, spearman, kendall(python示例实现)
时间: 2023-08-16 12:10:07 浏览: 182
好的,下面是示例代码实现:
首先,导入必要的库和数据集:
```python
import numpy as np
import pandas as pd
from scipy.stats import pearsonr, spearmanr, kendalltau
# 生成一些随机数据
np.random.seed(100)
df = pd.DataFrame({'A': np.random.randn(100),
'B': np.random.randint(0, 10, 100),
'C': np.random.uniform(0, 1, 100)})
```
接下来,分别使用pearsonr、spearmanr和kendalltau函数计算三种相关系数:
```python
# 计算pearson相关系数
pearson_corr, pearson_p_value = pearsonr(df['A'], df['B'])
print('Pearson correlation coefficient: {:.3f}, p-value: {:.3f}'.format(pearson_corr, pearson_p_value))
# 计算spearman相关系数
spearman_corr, spearman_p_value = spearmanr(df['B'], df['C'])
print('Spearman correlation coefficient: {:.3f}, p-value: {:.3f}'.format(spearman_corr, spearman_p_value))
# 计算kendall相关系数
kendall_corr, kendall_p_value = kendalltau(df['A'], df['C'])
print('Kendall correlation coefficient: {:.3f}, p-value: {:.3f}'.format(kendall_corr, kendall_p_value))
```
输出结果如下:
```
Pearson correlation coefficient: -0.037, p-value: 0.696
Spearman correlation coefficient: -0.020, p-value: 0.820
Kendall correlation coefficient: -0.023, p-value: 0.783
```
这里使用了一些随机生成的数据,结果很接近于0,说明三种相关系数都没有找到明显的相关性。但是,你可以用自己的数据集来进行计算。
阅读全文