-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
44 lines (31 loc) · 1.36 KB
/
utils.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
37
38
39
40
41
42
43
import pandas as pd
import numpy as np
def infer_from(model, X, sensitive_features=None):
"""Infer from binary-label model."""
kwargs = {}
if sensitive_features is not None:
kwargs = dict(sensitive_features=sensitive_features)
if hasattr(model, "predict_proba"):
infer_fn = lambda x: model.predict_proba(x)[:, 1]
elif hasattr(model, "predict"):
infer_fn = lambda x, **kwargs: model._pmf_predict(x, **kwargs)[:, 1]
return infer_fn(X.values if isinstance(X, pd.DataFrame) else X, **kwargs)
def score(model, X, y_true, sensitive_features=None):
kwargs = {}
if sensitive_features is not None:
kwargs = dict(sensitive_features=sensitive_features)
y_pred = model.predict(X.values if isinstance(X, pd.DataFrame) else X,
**kwargs)
return (y_pred == y_true).mean()
def log_losses(y_true, y_pred, eps=1e-15):
if not isinstance(y_pred, np.ndarray):
y_pred = np.array(y_pred)
if not isinstance(y_true, np.ndarray):
y_true = np.array(y_true)
y_pred = np.clip(y_pred, eps, 1 - eps)
one_minus_y_pred = np.clip(1 - y_pred, eps, 1 - eps)
return -(y_true * np.log(y_pred) + (1 - y_true) * np.log(one_minus_y_pred))
def normalize_vuln_mean(vuln_vals):
return (2 * vuln_vals.mean() - 1) * 100
def normalize_vuln_std(vuln_vals):
return 2 * vuln_vals.std() * 100