def global_f_score(Theta_true, Theta_est, beta=1, eps=1e-6):
"""In line with `global_precision` and `global_recall`, compute the
global f score given true and estimated graphical structures. The
f score has the only parameter beta.
Parameters
----------
Theta_true : 3D ndarray, shape (timesteps, n_vertices, n_vertices)
Theta_est : 3D ndarray, shape (timesteps, n_vertices, n_vertices)
beta : float (default 1)
beta value of the F score to be computed
eps : float
per_ts : bool
whether to compute average or per timestep recall
Returns
-------
float f-beta score
"""
assert Theta_est.shape == Theta_true.shape
d = Theta_true.shape[1]
n = len(Theta_est)
tps = fps = fns = tns = 0
for i in range(n):
est_edges = set(get_edges(Theta_est[i], eps))
gt_edges = set(get_edges(Theta_true[i], eps))
n_joint = len(est_edges.intersection(gt_edges))
tps += n_joint
fps += len(est_edges) - n_joint
fns += len(gt_edges) - n_joint
tns += d**2 - d - tps - fps - fns
nom = (1 + beta**2) * tps
denom = nom + beta**2 * fns + fps
with np.errstate(divide='ignore', invalid='ignore'):
f = np.nan_to_num(np.true_divide(nom, denom))
return f
评论列表
文章目录