python分析多个特征的相关性并绘制热力图

首先先得到分析的数据

  • 引入分析数据的3大库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
  • 这里已经提前获取好分析数据了,存于df
#打印数据 df
print(df)
#             x10        x11        x12       x13       x14       x15       x16       x17       x18       x19
# 0     49.895756  17.775994   5.270920  0.771761  0.018632  0.006864  0.003923  0.003923  0.486903  0.100025
# 1     57.709936  23.799994   3.325423  0.234185  0.003903  0.003903  0.003903  0.003903  0.520908  0.144414
# 2     55.831441  27.993933  12.687485  4.852282  1.393889  0.373252  0.041817  0.007744  0.530904  0.128548
# 3     40.467228  18.445954   9.118901  3.079428  0.840261  0.272434  0.007653  0.001531  0.483284  0.114790
# 4     18.026254   8.570709   0.410381  0.000000  0.000000  0.000000  0.000000  0.000000  0.475935  0.123572
# ...         ...        ...        ...       ...       ...       ...       ...       ...       ...       ...
# 1146   6.071765   0.937472   0.031145  0.003115  0.000000  0.000000  0.000000  0.000000  0.537470  0.116795
# 1147  63.197145  27.377668   8.067688  0.979548  0.001552  0.000000  0.000000  0.000000  0.516733  0.124190
# 1148  30.461898  13.966980   1.763305  0.137858  0.011221  0.000000  0.000000  0.000000  0.560632  0.129843
# 1149  40.525739  12.604947   4.740919  1.077570  0.563518  0.326860  0.239568  0.174584  0.485972  0.106690
# 1150  69.423565   7.031843   1.750548  0.046597  0.021180  0.008472  0.000000  0.000000  0.556192  0.088957

分析各个特征的之间的相关性

  • 这里直接使用df.corr()
#对df进行相关性分析
new_df = df.corr()
print(new_df)#打印值
#           x10       x11       x12       x13       x14       x15       x16       x17 
# x10  1.000000  0.767091  0.763409  0.486606  0.163915  0.132227  0.114722  0.084682 
# x11  0.767091  1.000000  0.919589  0.624537  0.257159  0.216127  0.181431  0.139196 
# x12  0.763409  0.919589  1.000000  0.780891  0.383977  0.328839  0.273616  0.214250 
# x13  0.486606  0.624537  0.780891  1.000000  0.825772  0.758195  0.636787  0.518501 
# x14  0.163915  0.257159  0.383977  0.825772  1.000000  0.931661  0.772151  0.625245 
# x15  0.132227  0.216127  0.328839  0.758195  0.931661  1.000000  0.906374  0.782185 
# x16  0.114722  0.181431  0.273616  0.636787  0.772151  0.906374  1.000000  0.943460 
# x17  0.084682  0.139196  0.214250  0.518501  0.625245  0.782185  0.943460  1.000000 
# x18 -0.086155 -0.128990 -0.128006 -0.167954 -0.149553 -0.150996 -0.122688 -0.097073 
# x19 -0.086474 -0.090721 -0.098866 -0.090738 -0.058645 -0.060441 -0.039783 -0.019091 
#           x18       x19
# x10 -0.086155 -0.086474
# x11 -0.128990 -0.090721
# x12 -0.128006 -0.098866
# x13 -0.167954 -0.090738
# x14 -0.149553 -0.058645
# x15 -0.150996 -0.060441
# x16 -0.122688 -0.039783
# x17 -0.097073 -0.019091
# x18  1.000000 -0.131497
# x19 -0.131497  1.000000

绘制其热力图

  • 这里使用了seaborn库,如果没有直接在在终端pip install seaborn下载

注:本人使用的是 vscode

import seaborn as sns  
#引入seaborn库
plt.figure(1)
sns.heatmap(new_df,annot=True, vmax=1, square=True)#绘制new_df的矩阵热力图
plt.show()#显示图片
  • 效果图:
    python分析多个特征的相关性并绘制热力图_第1张图片
  • 如果觉得颜色不喜欢,可以加入cmap属性,可选值入下:

‘Accent’, ‘Accent_r’, ‘Blues’, ‘Blues_r’, ‘BrBG’, ‘BrBG_r’, ‘BuGn’, ‘BuGn_r’, ‘BuPu’, ‘BuPu_r’, ‘CMRmap’,
‘CMRmap_r’, ‘Dark2’, ‘Dark2_r’, ‘GnBu’, ‘GnBu_r’, ‘Greens’, ‘Greens_r’, ‘Greys’, ‘Greys_r’, ‘OrRd’, ‘OrRd_r’, ‘Oranges’, ‘Oranges_r’, ‘PRGn’, ‘PRGn_r’, ‘Paired’, ‘Paired_r’, ‘Pastel1’, ‘Pastel1_r’, ‘Pastel2’, ‘Pastel2_r’, ‘PiYG’, ‘PiYG_r’, ‘PuBu’, ‘PuBuGn’, ‘PuBuGn_r’, ‘PuBu_r’, ‘PuOr’, ‘PuOr_r’, ‘PuRd’, ‘PuRd_r’, ‘Purples’, ‘Purples_r’, ‘RdBu’, ‘RdBu_r’, ‘RdGy’, ‘RdGy_r’, ‘RdPu’, ‘RdPu_r’, ‘RdYlBu’, ‘RdYlBu_r’, ‘RdYlGn’, ‘RdYlGn_r’, ‘Reds’, ‘Reds_r’, ‘Set1’, ‘Set1_r’, ‘Set2’, ‘Set2_r’, ‘Set3’, ‘Set3_r’, ‘Spectral’, ‘Spectral_r’, ‘Wistia’, ‘Wistia_r’, ‘YlGn’, ‘YlGnBu’, ‘YlGnBu_r’, ‘YlGn_r’, ‘YlOrBr’, ‘YlOrBr_r’, ‘YlOrRd’, ‘YlOrRd_r’, ‘afmhot’, ‘afmhot_r’, ‘autumn’, ‘autumn_r’, ‘binary’, ‘binary_r’,
‘bone’, ‘bone_r’, ‘brg’, ‘brg_r’, ‘bwr’, ‘bwr_r’, ‘cividis’, ‘cividis_r’, ‘cool’, ‘cool_r’, ‘coolwarm’, ‘coolwarm_r’, ‘copper’, ‘copper_r’, ‘crest’, ‘crest_r’, ‘cubehelix’, ‘cubehelix_r’, ‘flag’, ‘flag_r’, ‘flare’, ‘flare_r’, ‘gist_earth’, ‘gist_earth_r’, ‘gist_gray’, ‘gist_gray_r’, ‘gist_heat’, ‘gist_heat_r’, ‘gist_ncar’, ‘gist_ncar_r’, ‘gist_rainbow’, ‘gist_rainbow_r’, ‘gist_stern’, ‘gist_stern_r’, ‘gist_yarg’, ‘gist_yarg_r’, ‘gnuplot’, ‘gnuplot2’, ‘gnuplot2_r’, ‘gnuplot_r’, ‘gray’, ‘gray_r’, ‘hot’, ‘hot_r’, ‘hsv’, ‘hsv_r’,‘plasma’, ‘plasma_r’, ‘prism’, ‘prism_r’, ‘rainbow’, ‘rainbow_r’, ‘rocket’, ‘rocket_r’, ‘seismic’, ‘seismic_r’, ‘spring’, ‘spring_r’, ‘summer’, ‘summer_r’, ‘tab10’, ‘tab10_r’, ‘tab20’, ‘tab20_r’, ‘tab20b’, ‘tab20b_r’, ‘tab20c’, ‘tab20c_r’, ‘terrain’, ‘terrain_r’, ‘turbo’, ‘turbo_r’, ‘twilight’, ‘twilight_r’, ‘twilight_shifted’, ‘twilight_shifted_r’, ‘viridis’, ‘viridis_r’, ‘vlag’, ‘vlag_r’, ‘winter’, ‘winter_r’

  • 下面举例加入cmap=‘Accent’属性:
import seaborn as sns  
#引入seaborn库
plt.figure(1)
sns.heatmap(new_df,annot=True, vmax=1, square=True,cmap='Accent')#绘制new_df的矩阵热力图
plt.show()#显示图片

python分析多个特征的相关性并绘制热力图_第2张图片

你可能感兴趣的:(vscode,python,python)