Skip to content

Instantly share code, notes, and snippets.

@dayyass
Created October 9, 2021 14:01
Show Gist options
  • Save dayyass/005224c5768ed1c3cf573a29e15475e2 to your computer and use it in GitHub Desktop.
Save dayyass/005224c5768ed1c3cf573a29e15475e2 to your computer and use it in GitHub Desktop.
Find a labels mapper with the highest accuracy.
from itertools import permutations
import numpy as np
from sklearn.metrics import accuracy_score
np.random.seed(42)
y_true = np.random.randint(low=0, high=3, size=100)
noize_mapper = {0: 1, 1: 2, 2: 0}
y_pred = np.array([noize_mapper[label] for label in y_true])
for labels in permutations(set(y_pred)):
mapper = {i: label for i, label in enumerate(labels)}
y_pred_new = np.array([mapper[label] for label in y_pred])
accuracy = accuracy_score(
y_true=y_true,
y_pred=y_pred_new,
)
print(f"mapper: {mapper}\naccuracy: {accuracy}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment