-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
52 lines (47 loc) · 1.68 KB
/
evaluation.py
File metadata and controls
52 lines (47 loc) · 1.68 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
40
41
42
43
44
45
46
47
48
49
50
51
import re
import math
import numpy
def evaluate(boundary, target_boundry, err_tolerance = 2):
tp = 0
fp = 0
fn = 0
for i in range(1, len(target_boundry)):
if target_boundry[i] != 0:
flag = False
for j in range(i-err_tolerance, i+err_tolerance):
if boundary[j] != 0:
flag = True
break
if flag:
tp+=1
else:
fn+=1
total = sum([0 if boundary[i]==0 else 1 for i in range(len(boundary))])
total2 = sum([0 if target_boundry[i]==0 else 1 for i in range(len(target_boundry))])
fp = total - tp
# print (total, total2, tp, fp, fn)
precision = 1.0 * tp / (fp + tp)
recall = 1.0 * tp / (fn + tp)
if precision == 0 and recall == 0:
f1 = 0
else:
f1 = 2*precision*recall/(precision+recall)
return precision, recall, f1
def baseline(target_boundry):
precisions = []
recalls = []
f1s = []
boundary_len = len(target_boundry)
num_of_boundry = sum([0 if target_boundry[i]==0 else 1 for i in range(len(target_boundry))])
for i in range(1000):
baseline = numpy.random.choice(len(target_boundry), num_of_boundry)
# print (baseline)
baseline_boundry = [0] * boundary_len
for b in baseline:
baseline_boundry[b] = 1
precision, recall, f1 = evaluate(baseline_boundry,target_boundry)
precisions.append(precision)
recalls.append(recall)
f1s.append(f1)
print (sum(precisions)/ len(precisions), sum(recalls)/ len(recalls), sum(f1s)/ len(f1s))
return sum(precisions)/ len(precisions), sum(recalls)/ len(recalls), sum(f1s)/ len(f1s)