• 【生物信息学】使用皮尔逊相关系数进行相关性分析


    目录

    一、实验介绍

    二、实验环境

    1. 配置虚拟环境

    2. 库版本介绍

    3. IDE

    三、实验内容

    0. 导入必要的工具

    1. cal_pearson(计算皮尔逊相关系数)

    2. 主程序

    a. 实验1(较强的正相关关系):

    b. 实验2(几乎没有线性相关关系):

    c. 实验3(非常强的正相关关系):

    d. 实验4(斯皮尔曼相关系数矩阵):

    3. 代码整合


    一、实验介绍

            本实验主要实现了自定义皮尔逊相关系数进行相关性分析。

            相关性分析是一种常用的统计方法,用于评估两个或多个变量之间的关联程度。在本实验中,我们使用了皮尔逊相关系数和斯皮尔曼相关系数这两种常见的相关性指标。皮尔逊相关系数用于度量两个连续变量之间的线性关系,而斯皮尔曼相关系数则适用于评估两个变量之间的任何单调关系,无论是否线性。

    二、实验环境

        本系列实验使用了PyTorch深度学习框架,相关操作如下(基于深度学习系列文章的环境):

    1. 配置虚拟环境

    深度学习系列文章的环境

    conda create -n DL python=3.7 
    conda activate DL
    pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
    
    conda install matplotlib
    conda install scikit-learn

    新增加

    conda install pandas
    conda install seaborn
    conda install networkx
    conda install statsmodels
    pip install pyHSICLasso

    注:本人的实验环境按照上述顺序安装各种库,若想尝试一起安装(天知道会不会出问题)

    2. 库版本介绍

    软件包本实验版本目前最新版
    matplotlib3.5.33.8.0
    numpy1.21.61.26.0
    python3.7.16
    scikit-learn0.22.11.3.0
    torch1.8.1+cu1022.0.1
    torchaudio0.8.12.0.2
    torchvision0.9.1+cu1020.15.2

    新增

    networkx2.6.33.1
    pandas1.2.32.1.1
    pyHSICLasso1.4.21.4.2
    seaborn0.12.20.13.0
    statsmodels0.13.50.14.0

    3. IDE

            建议使用Pycharm(其中,pyHSICLasso库在VScode出错,尚未找到解决办法……)

    win11 安装 Anaconda(2022.10)+pycharm(2022.3/2023.1.4)+配置虚拟环境_QomolangmaH的博客-CSDN博客https://blog.csdn.net/m0_63834988/article/details/128693741https://blog.csdn.net/m0_63834988/article/details/128693741icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/128693741

    三、实验内容

    0. 导入必要的工具

    1. import numpy as np
    2. from scipy import stats
    3. import matplotlib.pyplot as plt

    1. cal_pearson(计算皮尔逊相关系数)

    1. def cal_pearson(x, y):
    2. n = len(x)
    3. mean_x = np.mean(x)
    4. mean_y = np.mean(y)
    5. x_ = x - mean_x
    6. y_ = y - mean_y
    7. s_x = np.sqrt(np.sum(x_ * x_))
    8. s_y = np.sqrt(np.sum(y_ * y_))
    9. r = np.sum((x_ / s_x) * (y_ / s_y))
    10. t = r / np.sqrt((1 - r * r) / (n - 2)) # p值需要对t值进行查表获得
    11. return r
    • 计算变量 x 的长度,即样本个数。

    • 计算变量 x 、 y 的均值。

    • 计算变量 x、 y 的标准差。

    • 计算皮尔逊相关系数 r,即将 x_ 和 y_ 中对应位置的值相除,然后相乘后求和。

    • 计算 t 值,即将 r 的值除以 sqrt((1 - r^2) / (n - 2))。这里的 n - 2 是修正因子,用于校正样本量对 t 值的影响。

    • 返回计算得到的皮尔逊相关系数 r。

    2. 主程序

    a. 实验1(较强的正相关关系):

    1. x1 = np.random.random(100)
    2. y1 = np.random.random(100) + x1
    3. plt.scatter(x1, y1, marker='.')
    4. plt.show()
    5. pearson1, p1 = stats.pearsonr(x1, y1)
    6. r1 = cal_pearson(x1, y1)
    7. print(pearson1)
    8. print(r1)
    9. print()
    • 生成两个长度为100的随机数组x1和y1,其中y1是在x1的基础上加上一些随机噪声。
    • 绘制x1和y1的散点图。
    • 使用scipy.stats.pearsonr函数计算了x1和y1的皮尔逊相关系数和p值,
    • 使用自定义的cal_pearson函数计算了相同的相关系数。
    1. 0.6991720710419989
    2. 0.6991720710419991

    b. 实验2(几乎没有线性相关关系):

    1. x2 = np.random.random(100)
    2. y2 = np.random.random(100)
    3. plt.scatter(x2, y2, marker='.')
    4. plt.show()
    5. pearson2, p2 = stats.pearsonr(x2, y2)
    6. r2 = cal_pearson(x2, y2)
    7. print(pearson2)
    8. print(r2)
    9. print()

            生成了两个长度为100的随机数组x2和y2,没有加入噪声。同样绘制了散点图,并分别计算了皮尔逊相关系数。

    1. -0.11511730616773974
    2. -0.11511730616773967

    c. 实验3(非常强的正相关关系):

            生成了两个长度为100的随机数组x3和y3,其中y3是在x3的基础上加上一些较大的随机噪声。同样绘制了散点图,并分别计算了皮尔逊相关系数。

    1. x3 = np.random.random(100)
    2. y3 = np.random.random(100) + x3 * 50
    3. plt.scatter(x3, y3, marker='.')
    4. plt.show()
    5. pearson3, p3 = stats.pearsonr(x3, y3)
    6. r3 = cal_pearson(x3, y3)
    7. print(pearson3)
    8. print(r3)
    9. print()

    d. 实验4(斯皮尔曼相关系数矩阵):

            生成了一个形状为(10, 10)的随机数组data,使用scipy.stats.spearmanr函数计算了data中各列之间的斯皮尔曼相关系数和p值,并将结果打印输出。

    1. data = np.random.random((10, 10))
    2. spearman_np, p_np = stats.spearmanr(data)
    3. print(spearman_np, p_np)

    3. 代码整合

    1. import numpy as np
    2. from scipy import stats
    3. import matplotlib.pyplot as plt
    4. def cal_pearson(x, y):
    5. n = len(x)
    6. mean_x = np.mean(x)
    7. mean_y = np.mean(y)
    8. x_ = x - mean_x
    9. y_ = y - mean_y
    10. s_x = np.sqrt(np.sum(x_ * x_))
    11. s_y = np.sqrt(np.sum(y_ * y_))
    12. r = np.sum((x_ / s_x) * (y_ / s_y))
    13. t = r / np.sqrt((1 - r * r) / (n - 2)) # p值需要对t值进行查表获得
    14. return r
    15. if __name__ == '__main__':
    16. np.random.seed(0)
    17. x1 = np.random.random(100)
    18. y1 = np.random.random(100) + x1
    19. plt.scatter(x1, y1, marker='.')
    20. plt.show()
    21. pearson1, p1 = stats.pearsonr(x1, y1)
    22. r1 = cal_pearson(x1, y1)
    23. print(pearson1)
    24. print(r1)
    25. print()
    26. x2 = np.random.random(100)
    27. y2 = np.random.random(100)
    28. plt.scatter(x2, y2, marker='.')
    29. plt.show()
    30. pearson2, p2 = stats.pearsonr(x2, y2)
    31. r2 = cal_pearson(x2, y2)
    32. print(pearson2)
    33. print(r2)
    34. print()
    35. x3 = np.random.random(100)
    36. y3 = np.random.random(100) + x3 * 50
    37. plt.scatter(x3, y3, marker='.')
    38. plt.show()
    39. pearson3, p3 = stats.pearsonr(x3, y3)
    40. r3 = cal_pearson(x3, y3)
    41. print(pearson3)
    42. print(r3)
    43. print()
    44. data = np.random.random((10, 10))
    45. spearman_np, p_np = stats.spearmanr(data)
    46. print(spearman_np, p_np)

  • 相关阅读:
    idea插件(四)-- GsonFormatPlus(JSON对象转化JavaBean对象)
    Redis 内存优化神技,小内存保存大数据
    no declaration can be found for element ‘rabbit:connection-factory‘
    C# 设计模式 行为型模式 之 解释器模式
    SPA项目之主页面--数据表格的增删改查
    二分法,平衡二叉树、B树、B+树
    Conda管理Python不同版本教程
    腾讯被曝要求员工还清90万房贷再离职;苹果因不附带充电器被判赔偿消费者7000元;Git 2.6发布|极客头条
    Java学习笔记之基本数据类型转换
    人车目标检测竞赛-赛题分享
  • 原文地址:https://blog.csdn.net/m0_63834988/article/details/133497929