-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoring.py
36 lines (29 loc) · 1.17 KB
/
scoring.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
32
33
34
35
36
import numpy as np
from sklearn.metrics import balanced_accuracy_score, confusion_matrix, f1_score, accuracy_score, recall_score, precision_score, matthews_corrcoef
# balanced_accuracy With adjusted=True = Youden
def youden(y_true, y_pred):
return balanced_accuracy_score(y_true, y_pred, adjusted=True)
def specificity(y_true, y_pred):
tn, fp, _, _ = confusion_matrix(y_true, y_pred).ravel()
return tn / (tn + fp)
DEFAULT_SCORING_DICT = {
'recall': recall_score,
'precision': precision_score,
'f1': f1_score,
'accuracy': accuracy_score,
'specificity': specificity,
'matthews_corrcoef': matthews_corrcoef,
'youden': youden
}
def weighted_avg_std(values, weights):
"""
Compute the weighted average and standard deviation of a given set of values and weights.
Parameters:
values (array-like): The data values.
weights (array-like): The weights corresponding to the data values.
Returns:
tuple(float, float): The weighted average and standard deviation.
"""
average = np.average(values, weights=weights)
variance = np.average((values-average)**2, weights=weights)
return average, np.sqrt(variance)