全国大学生数学建模大赛备赛——相关系数的求解(皮尔逊(pearson)、斯皮尔曼(spearman)、肯德尔(kendall)相关系数)

相关系数是用来衡量两个变量之间线性相关程度的指标。它的取值范围在-1到1之间,当相关系数为1时表示两个变量完全正相关(即一个变大另一个也变大),当相关系数为-1时表示两个变量完全负相关(即一个变大另一个变小),当相关系数为0时表示两个变量不存在线性相关性。

常用的相关系数有 Pearson 相关系数(用于衡量两个连续变量之间的线性相关性),Spearman 相关系数(用于衡量两个变量之间的等级相关性)等。相关系数的计算可以帮助我们了解数据之间的关系,对于统计分析和建模非常重要。

皮尔逊(pearson):

皮尔逊相关系数(Pearson correlation coefficient)衡量了两个连续变量之间的线性关系强度和方向。取值范围在-1到1之间,-1表示完全负相关,1表示完全正相关,0表示无线性相关

计算公式:

  1. 皮尔逊相关系数适用场景:

    • 适用于连续变量之间的线性关系分析,例如:身高和体重之间的相关性分析、温度和销售额之间的相关性分析等。
    • 适用于正态分布的数据或者满足线性关系的数据。

斯皮尔曼(spearman):

斯皮尔曼相关系数(Spearman's rank correlation coefficient)用于度量两个变量之间的等级关系的强度和方向。它通过将变量的观测值转换为等级,然后计算这些等级之间的皮尔逊相关系数来实现。

计算公式:

  1. 斯皮尔曼相关系数适用场景:

    • 适用于两个变量之间的等级关系分析,不要求数据满足线性关系。
    • 适用于数据不满足正态分布或存在异常值的情况,更具有鲁棒性。

肯德尔(kendall):

肯德尔相关系数(Kendall rank correlation coefficient)也用于测量变量之间的等级关系。与斯皮尔曼相关系数类似,它使用变量的等级而不是实际值,来计算两个变量之间的相关性。

计算公式;

  1. 肯德尔相关系数适用场景:

    • 适用于两个变量之间的等级关系分析,也不要求数据满足线性关系。
    • 在样本数据较小时或存在重复数据时,肯德尔相关系数通常比较适用。
    • 对异常值相对比较敏感的情况下,肯德尔相关系数也更可靠。

这三种相关系数都是用来度量变量之间关系的强度和方向,但适用于不同类型的数据和假设。

实例数据集:

|-----------------------|----------------------|-----------------------|----------------------|------------|
| sepal length (cm) | sep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6.7 | 3.1 | 5.6 | 2.4 | 2 |
| 6.9 | 3.1 | 5.1 | 2.3 | 2 |
| 5.8 | 2.7 | 5.1 | 1.9 | 2 |
| 6.8 | 3.2 | 5.9 | 2.3 | 2 |
| 6.7 | 3.3 | 5.7 | 2.5 | 2 |
| 6.7 | 3 | 5.2 | 2.3 | 2 |
| 6.3 | 2.5 | 5 | 1.9 | 2 |
| 6.5 | 3 | 5.2 | 2 | 2 |
| 6.2 | 3.4 | 5.4 | 2.3 | 2 |
| 5.9 | 3 | 5.1 | 1.8 | 2 |

这里以鸾尾花的数据集为例

皮尔逊(pearson)

代码实现:

复制代码
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets
# 加载鸢尾花数据集
iris = datasets.load_iris()
# 转换为 DataFrame
iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
# 计算相关系数(pearson方法)
iris_pearson = iris_df.corr(method='pearson')
#输出计算结果
print(iris_pearson)
#生成特征热力图
plt.figure(figsize=(10,8))
sns.heatmap(iris_df.corr(method='kendall'),cmap="coolwarm",annot=True)
plt.show()

图片:

斯皮尔曼(spearman)

代码:

python 复制代码
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets
# 加载鸢尾花数据集
iris = datasets.load_iris()
# 转换为 DataFrame
iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
# 计算相关系数(spearman方法)
iris_pearson = iris_df.corr(method='spearman')
#输出计算结果
print(iris_pearson)
#生成特征热力图
plt.figure(figsize=(10,8))
sns.heatmap(iris_df.corr(method='kendall'),cmap="cool",annot=True)
plt.show()

图片:

肯德尔(kendall)相关系数

代码:

python 复制代码
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import datasets
# 加载鸢尾花数据集
iris = datasets.load_iris()
# 转换为 DataFrame
iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
# 计算相关系数(kendall方法)
iris_pearson = iris_df.corr(method='kendall')
#输出计算结果
print(iris_pearson)
#生成特征热力图
plt.figure(figsize=(10,8))
sns.heatmap(iris_df.corr(method='kendall'),cmap="Blues_r",annot=True)
plt.show()

图片:

相关推荐
爱打代码的小林11 分钟前
基于 MediaPipe 实现实时面部关键点检测
python·opencv·计算机视觉
极客小云29 分钟前
【ComfyUI API 自动化利器:comfyui_xy Python 库使用详解】
网络·python·自动化·comfyui
闲人编程1 小时前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
痴儿哈哈1 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
花酒锄作田1 小时前
SQLAlchemy中使用UPSERT
python·sqlalchemy
SoleMotive.1 小时前
一个准程序员的健身日志:用算法调试我的增肌计划
python·程序员·健身·职业转型
亓才孓1 小时前
[Properties]写配置文件前,必须初始化Properties(引用变量没执行有效对象,调用方法会报空指针错误)
开发语言·python
Bruk.Liu1 小时前
(LangChain 实战14):基于 ChatMessageHistory 自定义实现对话记忆功能
人工智能·python·langchain·agent
大江东去浪淘尽千古风流人物2 小时前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
Swift社区2 小时前
Gunicorn 与 Uvicorn 部署 Python 后端详解
开发语言·python·gunicorn