-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
39 lines (27 loc) · 1.17 KB
/
metrics.py
File metadata and controls
39 lines (27 loc) · 1.17 KB
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
37
38
39
from typing import Callable
import numpy as np
from emukit.core import ParameterSpace
from sklearn.metrics import average_precision_score
from sklearn.metrics import f1_score
from models import Model
from test_functions import nan_mask_function
def create_f1_evaluator(
space: ParameterSpace, performance: Callable[[np.ndarray], np.ndarray], fidelity: int = 10**5
) -> Callable[[Model], float]:
x = space.sample_uniform(fidelity)
nan_masked_performance = nan_mask_function(performance)
actual_class = nan_masked_performance(x) < 0
def f1_score_fn(model: Model) -> float:
classification = model.classify(x)
return f1_score(actual_class, classification)
return f1_score_fn
def avg_precision(
space: ParameterSpace, performance: Callable[[np.ndarray], np.ndarray], fidelity: int = 10**5
) -> Callable[[Model], float]:
x = space.sample_uniform(fidelity)
nan_masked_performance = nan_mask_function(performance)
actual_class = nan_masked_performance(x) < 0
def fn(model: Model) -> float:
class_probability = model.class_probability(x)
return average_precision_score(actual_class, class_probability)
return fn