python 存储在pandas数据帧中的几个变量之间的矩阵相关性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 存储在pandas数据帧中的几个变量之间的矩阵相关性相关的知识,希望对你有一定的参考价值。

## WAY 1

def correlation_matrix(df,title):
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import cm as cm

    # get name of variables
    lvariables = df.columns.values
    
    # create chart objects
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    cmap = cm.get_cmap('jet', 30)
    # plot
    cax = ax1.imshow(df.corr(), interpolation="nearest", cmap=cmap)
    # set grid
    ax1.grid(True)
    # set title
    plt.title('MATRIX CORRELATION: %s'%title)
    # set axis ticks
    plt.xticks(range(0,len(lvariables),1),lvariables,fontsize=10, rotation='vertical')
    plt.yticks(range(0,len(lvariables),1),lvariables,fontsize=10)
    # build color bar
    cbar = fig.colorbar(cax)
    # display
    plt.show()
    
    
## WAY 2
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
scatter_matrix(DF, alpha=0.2, figsize=(6, 6), diagonal='kde')
plt.show()

以上是关于python 存储在pandas数据帧中的几个变量之间的矩阵相关性的主要内容,如果未能解决你的问题,请参考以下文章