forked from h2oai/driverlessai-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
true_negative_count.py
28 lines (25 loc) · 1.05 KB
/
true_negative_count.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""Optimizes for specific Confusion Matrix Values: `TN` - only recommended if threshold is adjusted"""
import typing
import numpy as np
from h2oaicore.metrics import CustomScorer
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix
class CMTrueNegative(CustomScorer):
_threshold = 0.5 # Example only, should be adjusted based on domain knowledge and other experiments
_description = "Increase true negative count"
_binary = True
_maximize = True
_perfect_score = 1e20
_display_name = "TN"
def score(self,
actual: np.array,
predicted: np.array,
sample_weight: typing.Optional[np.array] = None,
labels: typing.Optional[np.array] = None) -> float:
lb = LabelEncoder()
labels = lb.fit_transform(labels)
actual = lb.transform(actual)
predicted = (predicted > self._threshold)
cm = confusion_matrix(actual, predicted, sample_weight=sample_weight, labels=labels)
tn, fp, fn, tp = cm.ravel()
return tn