如何绘制带有每列和行描述的矩阵

huangapple go评论115阅读模式
英文:

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

问题

以下是您要翻译的内容:

  1. 要绘制dt_accuracy_mwlstm_accuracy_mw,以便可视化每个sigmaknots
sigma/knots 4  5  6  7
    0.2
    0.35    实际矩阵包含上述的准确性
    0.5
    0.65
  1. 上述的综合版本,每个条目包括dt_accuracy (ref_dt_accuracy - dt_accuracy)/lstm_accuracy (ref_lstm_accuracy - lstm_accuracy),因此每个条目包括dt_accuracy与参考值的差异以及相同的lstm_accuracy。然后,模型的每个准确性得分由/分隔。

要使用开源库如matplotlib、seaborn等实现这两个图形,您可以参考以下Python代码:

import matplotlib.pyplot as plt
import numpy as np

# 1. 绘制第一个矩阵
plt.figure(figsize=(8, 6))
plt.imshow(dt_accuracy_mw, cmap='viridis', origin='lower')
plt.colorbar()
plt.xticks(np.arange(len(knots)), knots)
plt.yticks(np.arange(len(sigma)), sigma)
plt.xlabel('knots')
plt.ylabel('sigma')
plt.title('dt_accuracy_mw')

# 2. 绘制第二个矩阵
combined_matrix = np.zeros_like(dt_accuracy_mw, dtype=float)
for i in range(len(sigma)):
    for j in range(len(knots)):
        combined_matrix[i, j] = f'{dt_accuracy_mw[i][j] - ref_dt_accuracy:.2f} / {lstm_accuracy_mw[i][j] - ref_lstm_accuracy:.2f}'

plt.figure(figsize=(8, 6))
plt.imshow(combined_matrix, cmap='coolwarm', origin='lower')
plt.colorbar()
plt.xticks(np.arange(len(knots)), knots)
plt.yticks(np.arange(len(sigma)), sigma)
plt.xlabel('knots')
plt.ylabel('sigma')
plt.title('Combined Matrix')

plt.show()

这段代码将帮助您绘制所需的两个矩阵。您可以根据需要进一步调整图形的样式和标签。

英文:

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.

答案1

得分: 3

你可以按照以下方式创建一个Seaborn热图:

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()

如果我正确理解第二个问题,一个注释矩阵可以完成任务(data可以是任何具有正确宽度和高度的内容):

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()
英文:

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()

如何绘制带有每列和行描述的矩阵

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()

如何绘制带有每列和行描述的矩阵

huangapple
  • 本文由 发表于 2023年2月16日 18:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定