From 39a39ba47e1e88fb42dffffeb002f970250b8a41 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Sun, 5 Nov 2023 19:41:51 +0100 Subject: [PATCH] Added weighted MCC --- src/suite.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/suite.py b/src/suite.py index 3725deb..d47e398 100644 --- a/src/suite.py +++ b/src/suite.py @@ -14,7 +14,7 @@ import json # OpenCV import numpy as np import cv2 -from sklearn.metrics import confusion_matrix +from sklearn.metrics import confusion_matrix, matthews_corrcoef from sklearn.preprocessing import ( MinMaxScaler, StandardScaler, @@ -395,21 +395,40 @@ class CVSuite: modelnr = (idx * 2) + idy guesses = self.models[modelnr][2] + # Get accuracy + guess_total = 0 + guess_ok = 0 for guess in guesses: - print(guess) + guess_total += 1 + if guess[0] == guess[1]: + guess_ok += 1 # Convert guess array tag_true = [guess[0] for guess in guesses ] tag_predict = [guess[1] for guess in guesses ] + # calculate weighted average + data_len_class = [tag_true.count(tag) for tag in range(0, 8)] + data_len = len(tag_true) + data_class_weight = [data_len_class[tag] / data_len for tag in range(0, 8)] + + tag_weighted = [] + for tag in tag_true: + tag_weighted.append(data_class_weight[tag]) + + print(C_DBUG, f"Data length per class: {data_len_class}; Total: {data_len}") + + # Get MCC + mcc = matthews_corrcoef(tag_true, tag_predict, sample_weight=tag_weighted) + labels = [Tree(tag).name for tag in range(0, 8)] cm = confusion_matrix(tag_true, tag_predict) cmn = cmn = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] sns.heatmap(cmn, xticklabels=labels, yticklabels=labels, ax=graph, annot=True, cbar=False, fmt='.2f') - graph.set_title(self.models[modelnr][0]) - graph.set_xlabel("Predicted") + graph.set_title(f"{self.models[modelnr][0]}; MCC: {mcc:.2f}; Acc: {((guess_ok / guess_total) * 100):.2f}%" ) + graph.set_xlabel("Predicted") graph.set_ylabel("Actual") graph.set_xticklabels(labels, rotation=0) graph.set_yticklabels(labels, rotation=0)