Pandas DataFrame 计算相关系数与绘制热力图

环境信息

  • ModelArts
    • Notebook - Multi-Engine 2.0 (python3)
      • JupyterLab - Notebook - Conda-python3
        • pandas 0.22.0

Pandas DataFrame 计算相关系数与绘制热力图

# pandas|numpy|matplotlib|seaborn
!pip list | grep -E 'pandas|numpy|matplotlib|seaborn'

matplotlib                         2.1.0    
numpy                              1.14.0   
numpydoc                           0.7.0    
pandas                             0.22.0   
seaborn                            0.8.1    
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame(np.random.randint(100,size=(10,10)))
df

# 计算相关系数
df.corr()

# 设置大小
plt.subplots(figsize=(8, 8))
# 热力图
# annot=True 显示数值
# round(2) 小数点2位
sns.heatmap(df.corr().round(2),annot=True)

help

help(df.corr)

Help on method corr in module pandas.core.frame:

corr(method='pearson', min_periods=1) method of pandas.core.frame.DataFrame instance
    Compute pairwise correlation of columns, excluding NA/null values
    
    Parameters
    ----------
    method : {'pearson', 'kendall', 'spearman'}
        * pearson : standard correlation coefficient
        * kendall : Kendall Tau correlation coefficient
        * spearman : Spearman rank correlation
    min_periods : int, optional
        Minimum number of observations required per pair of columns
        to have a valid result. Currently only available for pearson
        and spearman correlation
    
    Returns
    -------
    y : DataFrame

备注

  1. 欢迎各位同学一起来交流学习心得^_^
  2. 在线课程、沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
(完)