How to plot a matrix with description for each column and row

Question:

I have a data set I need to augment. Therefore, I have implemented an augmentation method called magnitude warping that has two hyperparameters to tune, namely sigma and knots. To assess the quality, I have two models that I train using the augmented data and test it on part of the real data. To compare the accuracy I also trained the models on only the real data. Lets assume the following Python code:

# test accuracy trained on real data only
ref_dt_accuracy = 0.86 
ref_lstm_accuracy = 0.85 

# test accuracy for each pair of hyperparameters
sigma = [0.2, 0.35, 0.5, 0.65]
knots = [4,5,6,7]

dt_accuracy_mw = [
[0.82, 0.85, 0.83, 0.84], 
[0.8, 0.79, 0.81, 0.79], 
[0.78,0.77, 0.74, 0.76], 
[0.74, 0.72, 0.78, 0.70]
]


lstm_accuracy_mw = [
[0.80, 0.83, 0.81, 0.82], 
[0.78, 0.77, 0.79, 0.77], 
[0.76,0.75, 0.72, 0.74], 
[0.72, 0.7, 0.76, 0.68]
]

Now, I want to plot two (three if the last option is possible) matrices:

  1. Plotting dt_accuracy_mw and lstm_accuracy_mw such that each sigma and knots are visualized:
sigma/knots 4  5  6  7
    0.2
    0.35    Actual matrix consisting of aforementioned accuracies
    0.5
    0.65
  1. A combined version of above such that each entry consists of dt_accuracy (ref_dt_accuracy - dt_accuracy)/lstm_accuracy (ref_lstm_accuracy - lstm_accuracy) , so each entry consists of the dt_accuracy the difference to the ref and the same for the lstm_accuracy. Each accuracy score of the models are then seperated by the /

How would one achieve this using any open source libraries such as matplotlib, seaborn etc.

Asked By: Unistack

||

Answers:

You can create a Seaborn heatmap as follows:

from matplotlib import pyplot as plt
import seaborn as sns

sigma = [0.2, 0.35, 0.5, 0.65]
knots = [4, 5, 6, 7]

dt_accuracy_mw = [[0.82, 0.85, 0.83, 0.84],
                  [0.8, 0.79, 0.81, 0.79],
                  [0.78, 0.77, 0.74, 0.76],
                  [0.74, 0.72, 0.78, 0.70]]

ax = sns.heatmap(data=dt_accuracy_mw, xticklabels=knots, yticklabels=sigma,
                 linewidths=1, linecolor='blue', clip_on=False, annot=True, cbar=False,
                 cmap=sns.color_palette(['white'], as_cmap=True))
ax.set_xlabel('knots')
ax.set_ylabel('sigma')
plt.tight_layout()
plt.show()

seaborn sns.heatmap colored white to show a matrix

If I understand the second question correctly, a matrix of annotations would do the job (the data can be anything with the correct width and height):

from matplotlib import pyplot as plt
import seaborn as sns

ref_dt_accuracy = 0.86
ref_lstm_accuracy = 0.85

sigma = [0.2, 0.35, 0.5, 0.65]
knots = [4, 5, 6, 7]

dt_accuracy_mw = [[0.82, 0.85, 0.83, 0.84],
                  [0.8, 0.79, 0.81, 0.79],
                  [0.78, 0.77, 0.74, 0.76],
                  [0.74, 0.72, 0.78, 0.70]]

lstm_accuracy_mw = [[0.80, 0.83, 0.81, 0.82],
                    [0.78, 0.77, 0.79, 0.77],
                    [0.76, 0.75, 0.72, 0.74],
                    [0.72, 0.7, 0.76, 0.68]]
annot_matrix = [[f'{ref_dt_accuracy - dt:.2f} / {ref_lstm_accuracy - lstm:.2f}'
                 for dt, lstm in zip(dt_row, lstm_row)]
                for dt_row, lstm_row in zip(dt_accuracy_mw, lstm_accuracy_mw)]

ax = sns.heatmap(data=dt_accuracy_mw, xticklabels=knots, yticklabels=sigma,
                 annot=annot_matrix, fmt='',
                 linewidths=2, linecolor='crimson', clip_on=False, cbar=False,
                 cmap=sns.color_palette(['aliceblue'], as_cmap=True))
ax.set_xlabel('knots')
ax.set_ylabel('sigma')
plt.tight_layout()
plt.show()

sns.heatmap to represent a text matrix

Answered By: JohanC