forked from h2oai/driverlessai-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfalse_discovery_rate.py
31 lines (28 loc) · 1.23 KB
/
false_discovery_rate.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
29
30
31
"""False Discovery Rate: `FP / (FP + TP)` for binary classification - 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 MyFalseDiscoveryRateScorer(CustomScorer):
_threshold = 0.1 # Example only, should be adjusted based on domain knowledge and other experiments
_description = "My False Discovery Rate for Binary Classification [threshold=%f]." % _threshold
_binary = True
_maximize = False
_perfect_score = 0
_display_name = "FDR"
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.__class__._threshold # probability -> label
cm = confusion_matrix(actual, predicted, sample_weight=sample_weight, labels=labels)
tn, fp, fn, tp = cm.ravel()
if (fp + tp) != 0:
return fp / (fp + tp)
else:
return 0