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

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

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

问题

以下是您要翻译的内容:

  1. 要绘制dt_accuracy_mwlstm_accuracy_mw,以便可视化每个sigmaknots
  1. sigma/knots 4 5 6 7
  2. 0.2
  3. 0.35 实际矩阵包含上述的准确性
  4. 0.5
  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代码:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # 1. 绘制第一个矩阵
  4. plt.figure(figsize=(8, 6))
  5. plt.imshow(dt_accuracy_mw, cmap='viridis', origin='lower')
  6. plt.colorbar()
  7. plt.xticks(np.arange(len(knots)), knots)
  8. plt.yticks(np.arange(len(sigma)), sigma)
  9. plt.xlabel('knots')
  10. plt.ylabel('sigma')
  11. plt.title('dt_accuracy_mw')
  12. # 2. 绘制第二个矩阵
  13. combined_matrix = np.zeros_like(dt_accuracy_mw, dtype=float)
  14. for i in range(len(sigma)):
  15. for j in range(len(knots)):
  16. combined_matrix[i, j] = f'{dt_accuracy_mw[i][j] - ref_dt_accuracy:.2f} / {lstm_accuracy_mw[i][j] - ref_lstm_accuracy:.2f}'
  17. plt.figure(figsize=(8, 6))
  18. plt.imshow(combined_matrix, cmap='coolwarm', origin='lower')
  19. plt.colorbar()
  20. plt.xticks(np.arange(len(knots)), knots)
  21. plt.yticks(np.arange(len(sigma)), sigma)
  22. plt.xlabel('knots')
  23. plt.ylabel('sigma')
  24. plt.title('Combined Matrix')
  25. 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:

  1. # test accuracy trained on real data only
  2. ref_dt_accuracy = 0.86
  3. ref_lstm_accuracy = 0.85
  4. # test accuracy for each pair of hyperparameters
  5. sigma = [0.2, 0.35, 0.5, 0.65]
  6. knots = [4,5,6,7]
  7. dt_accuracy_mw = [
  8. [0.82, 0.85, 0.83, 0.84],
  9. [0.8, 0.79, 0.81, 0.79],
  10. [0.78,0.77, 0.74, 0.76],
  11. [0.74, 0.72, 0.78, 0.70]
  12. ]
  13. lstm_accuracy_mw = [
  14. [0.80, 0.83, 0.81, 0.82],
  15. [0.78, 0.77, 0.79, 0.77],
  16. [0.76,0.75, 0.72, 0.74],
  17. [0.72, 0.7, 0.76, 0.68]
  18. ]

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:
  1. sigma/knots 4 5 6 7
  2. 0.2
  3. 0.35 Actual matrix consisting of aforementioned accuracies
  4. 0.5
  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热图:

  1. from matplotlib import pyplot as plt
  2. import seaborn as sns
  3. sigma = [0.2, 0.35, 0.5, 0.65]
  4. knots = [4, 5, 6, 7]
  5. dt_accuracy_mw = [[0.82, 0.85, 0.83, 0.84],
  6. [0.8, 0.79, 0.81, 0.79],
  7. [0.78, 0.77, 0.74, 0.76],
  8. [0.74, 0.72, 0.78, 0.70]]
  9. ax = sns.heatmap(data=dt_accuracy_mw, xticklabels=knots, yticklabels=sigma,
  10. linewidths=1, linecolor='blue', clip_on=False, annot=True, cbar=False,
  11. cmap=sns.color_palette(['white'], as_cmap=True))
  12. ax.set_xlabel('knots')
  13. ax set_ylabel('sigma')
  14. plt.tight_layout()
  15. plt.show()

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

  1. from matplotlib import pyplot as plt
  2. import seaborn as sns
  3. ref_dt_accuracy = 0.86
  4. ref_lstm_accuracy = 0.85
  5. sigma = [0.2, 0.35, 0.5, 0.65]
  6. knots = [4, 5, 6, 7]
  7. dt_accuracy_mw = [[0.82, 0.85, 0.83, 0.84],
  8. [0.8, 0.79, 0.81, 0.79],
  9. [0.78, 0.77, 0.74, 0.76],
  10. [0.74, 0.72, 0.78, 0.70]]
  11. lstm_accuracy_mw = [[0.80, 0.83, 0.81, 0.82],
  12. [0.78, 0.77, 0.79, 0.77],
  13. [0.76, 0.75, 0.72, 0.74],
  14. [0.72, 0.7, 0.76, 0.68]]
  15. annot_matrix = [[f'{ref_dt_accuracy - dt:.2f} / {ref_lstm_accuracy - lstm:.2f}'
  16. for dt, lstm in zip(dt_row, lstm_row)]
  17. for dt_row, lstm_row in zip(dt_accuracy_mw, lstm_accuracy_mw)]
  18. ax = sns.heatmap(data=dt_accuracy_mw, xticklabels=knots, yticklabels=sigma,
  19. annot=annot_matrix, fmt='',
  20. linewidths=2, linecolor='crimson', clip_on=False, cbar=False,
  21. cmap=sns.color_palette(['aliceblue'], as_cmap=True))
  22. ax.set_xlabel('knots')
  23. ax set_ylabel('sigma')
  24. plt.tight_layout()
  25. plt.show()
英文:

You can create a Seaborn heatmap as follows:

  1. from matplotlib import pyplot as plt
  2. import seaborn as sns
  3. sigma = [0.2, 0.35, 0.5, 0.65]
  4. knots = [4, 5, 6, 7]
  5. dt_accuracy_mw = [[0.82, 0.85, 0.83, 0.84],
  6. [0.8, 0.79, 0.81, 0.79],
  7. [0.78, 0.77, 0.74, 0.76],
  8. [0.74, 0.72, 0.78, 0.70]]
  9. ax = sns.heatmap(data=dt_accuracy_mw, xticklabels=knots, yticklabels=sigma,
  10. linewidths=1, linecolor='blue', clip_on=False, annot=True, cbar=False,
  11. cmap=sns.color_palette(['white'], as_cmap=True))
  12. ax.set_xlabel('knots')
  13. ax.set_ylabel('sigma')
  14. plt.tight_layout()
  15. 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):

  1. from matplotlib import pyplot as plt
  2. import seaborn as sns
  3. ref_dt_accuracy = 0.86
  4. ref_lstm_accuracy = 0.85
  5. sigma = [0.2, 0.35, 0.5, 0.65]
  6. knots = [4, 5, 6, 7]
  7. dt_accuracy_mw = [[0.82, 0.85, 0.83, 0.84],
  8. [0.8, 0.79, 0.81, 0.79],
  9. [0.78, 0.77, 0.74, 0.76],
  10. [0.74, 0.72, 0.78, 0.70]]
  11. lstm_accuracy_mw = [[0.80, 0.83, 0.81, 0.82],
  12. [0.78, 0.77, 0.79, 0.77],
  13. [0.76, 0.75, 0.72, 0.74],
  14. [0.72, 0.7, 0.76, 0.68]]
  15. annot_matrix = [[f'{ref_dt_accuracy - dt:.2f} / {ref_lstm_accuracy - lstm:.2f}'
  16. for dt, lstm in zip(dt_row, lstm_row)]
  17. for dt_row, lstm_row in zip(dt_accuracy_mw, lstm_accuracy_mw)]
  18. ax = sns.heatmap(data=dt_accuracy_mw, xticklabels=knots, yticklabels=sigma,
  19. annot=annot_matrix, fmt='',
  20. linewidths=2, linecolor='crimson', clip_on=False, cbar=False,
  21. cmap=sns.color_palette(['aliceblue'], as_cmap=True))
  22. ax.set_xlabel('knots')
  23. ax.set_ylabel('sigma')
  24. plt.tight_layout()
  25. 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:

确定