导出相关矩阵(从函数)

数据挖掘 机器学习 特征选择 麻木的 matplotlib
2022-02-25 07:42:10

我有兴趣将相关矩阵导出到 csv。我试过使用 to.csv 功能,但我的矩阵是从一个函数中获得的,我得到一个“对象没有属性 'to_csv'”错误。我很感激任何指导。代码如下。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm as cm

def correlation_matrix(df):

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    cmap = cm.get_cmap('jet', 30)
    cax = ax1.imshow(df.corr(), interpolation="nearest", cmap=cmap)
    ax1.grid(True)
    plt.title('Feature Correlation')
    labels = None
    # Add colorbar, and make sure to specify tick locations to match desired ticklabels.
    cbar = fig.colorbar(cax, ticks=[-1.1, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6,.8,1])
    plt.show()

correlation_matrix(pd.DataFrame(np.c_[Xs,Y]))
1个回答

不要使用从您的函数中获得的矩阵,

它看起来correlation也是一个 DataFrame,所以你可以简单地使用to_csv

correlation_df(within your function itself).to_csv("C:\destinationfolder\file.csv")