How can I plot confusion matrix for a multiclass multilabel problem in a better way than this?

JanaR: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02eca00f78494b07d4b9266cf0a36fad318f2b3b.png" data-download-href="https://discuss.pytorch.org/uploads/default/02eca00f78494b07d4b9266cf0a36fad318f2b3b" title="8_0.2_0.03_withoutSNR=0"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02eca00f78494b07d4b9266cf0a36fad318f2b3b_2_690x351.png" alt="8_0.2_0.03_withoutSNR=0" data-base62-sha1="pRVaPaZPLUzIcZLrubjqdZe2mv" width="690" height="351" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02eca00f78494b07d4b9266cf0a36fad318f2b3b_2_690x351.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02eca00f78494b07d4b9266cf0a36fad318f2b3b_2_1035x526.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02eca00f78494b07d4b9266cf0a36fad318f2b3b_2_1380x702.png 2x" data-dominant-color="F3F6FA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">8_0.2_0.03_withoutSNR=0</span><span class="informations">1920×977 89.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Also, how can I modify the accuracy calculation, so it make more sense?<br>
Here is my code:</p>
<blockquote>
<p>my_metrics = get_metrics(pred, label, nb_classes=label.shape[1])<br>
cm = my_metrics.pop(‘c_mat’)<br>
pyplot.figure(figsize=(9,9))<br>
sn.heatmap(cm, cmap=‘Blues’, annot=True, fmt=“.2f”, center=None, vmin=0)</p>
</blockquote>
<blockquote></blockquote>
<p>def get_metrics(pred, target, nb_classes=14):</p>
<pre><code>## 0. Pre-process

tanh = nn.Sigmoid()
preds_bin = (tanh(pred) &gt; 0.5).long()          # one-hot-enc matrix [S DOAs]

## 1. Metrics
preds_bin_noSilence = preds_bin[:,1:]           # one-hot-enc matrix [DOAs]
labels_noSilence = target[:,1:]                      # one-hot-enc matrix [DOAs]


preds_indx  = torch.flatten( preds_bin * torch.arange(0, nb_classes, 1) ).long()      # index very long vector
labels_indx = torch.flatten( target    * torch.arange(0, nb_classes, 1) ).long()      # index very long vector


conf_matrix = torch.zeros(nb_classes, nb_classes)
for t, p in zip(labels_indx, preds_indx):
    conf_matrix[t, p] += 1


TP = conf_matrix.diag()

# accuracy = []
# precision = []
# recall = []

for c in range(nb_classes):
    idx = torch.ones(nb_classes).byte()
    idx[c] = 0
    # all non-class samples classified as non-class
    TN = conf_matrix[idx.nonzero()[:, None], idx.nonzero()].sum() #conf_matrix[idx[:, None], idx].sum() - conf_matrix[idx, c].sum()
    # all non-class samples classified as class
    FP = conf_matrix[idx, c].sum()
    # all class samples not classified as class
    FN = conf_matrix[c, idx].sum()

    # print(TN.shape, TN)
    # input('dfgdg')

accuracy = (TP + TN) / (TP + FP + TN + FN)
precision = TP / (TP + FP)
recall = TP / (TP + FN)


## 3. Return
return {"acc": accuracy.numpy(),       # 80
        "prec": precision.numpy(),     # 0/0
        "recall": recall.numpy(),      # 0
        "c_mat": conf_matrix.numpy()}
</code></pre>

ptrblck: <p>Based on your code it seems that you are creating a “standard” confusion matrix, which shows the confusion between every two classes. For a multi-label classification you might want to check e.g. <a href="https://scikit-learn.org/stable/modules/generated/sklearn.metrics.multilabel_confusion_matrix.html"><code>sklearn.metrics.multilabel_confusion_matrix</code></a>.</p>