# Exporting Correlation Matrix (from function)

I'm interested in exporting a correlation matrix to csv. I've tried using the to.csv functionality, but my matrix is obtained from a function and I get an "object has no attribute 'to_csv'" error. I appreciate any guidance. Code is below.

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

def correlation_matrix(df):

fig = plt.figure()
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]))


It looks like correlation is a DataFrame too, so you can simply use to_csv:
correlation_df(within your function itself).to_csv("C:\destinationfolder\file.csv")

• What I mean is when using df.corr() it returns a dataframe itself which can easily be exported to different extensions.. you are using numpy to do the same, that's why a matrix,.also you can directly plot Correlation matrices, there are inbuilt functions to do the same or just use the sns.heatmap.. Let me know if I am not clear again – Aditya Apr 10 '18 at 0:58