2
2
from seqeval .metrics .sequence_labeling import precision_recall_fscore_support
3
3
from azureml .automl .core .shared .constants import Metric
4
4
5
- def named_entity_recognition_report (predictions_file , test_data_file , use_str = True ):
6
-
5
+
6
+ def named_entity_recognition_report (predictions_file , test_data_file , use_str = True ):
7
+
7
8
# getting the predicted and true labels
8
9
def get_labels_from_file (file ):
9
-
10
+
10
11
labels = []
11
12
current_labels = []
12
13
with open (file ) as opened :
@@ -22,39 +23,65 @@ def get_labels_from_file(file):
22
23
23
24
labels .append (current_labels )
24
25
return labels
25
-
26
+
26
27
prediction_labels = get_labels_from_file (predictions_file )
27
28
true_labels = get_labels_from_file (test_data_file )
28
-
29
+
29
30
# accuracy
30
31
accuracy = accuracy_score (y_true = true_labels , y_pred = prediction_labels )
31
32
# micro averages
32
33
precision_micro , recall_micro , f1_micro , _ = precision_recall_fscore_support (
33
- y_true = true_labels , y_pred = prediction_labels , average = ' micro'
34
+ y_true = true_labels , y_pred = prediction_labels , average = " micro"
34
35
)
35
36
# macro averages
36
37
precision_macro , recall_macro , f1_macro , _ = precision_recall_fscore_support (
37
- y_true = true_labels , y_pred = prediction_labels , average = ' macro'
38
+ y_true = true_labels , y_pred = prediction_labels , average = " macro"
38
39
)
39
40
# weighted averages
40
- precision_weighted , recall_weighted , f1_weighted , _ = precision_recall_fscore_support (
41
- y_true = true_labels , y_pred = prediction_labels , average = 'weighted'
41
+ (
42
+ precision_weighted ,
43
+ recall_weighted ,
44
+ f1_weighted ,
45
+ _ ,
46
+ ) = precision_recall_fscore_support (
47
+ y_true = true_labels , y_pred = prediction_labels , average = "weighted"
42
48
)
43
-
49
+
44
50
if use_str :
45
51
results = Metric .Accuracy + "\t " + "{:.3f}" .format (accuracy ) + "\n "
46
52
results += "\t \t " + "F1-score" + "\t " + "Precision" + "\t " + "Recall" + "\n "
47
- results += "micro" + "\t \t " + "{:.3f}" .format (f1_micro ) + "\t \t " \
48
- + "{:.3f}" .format (precision_micro ) + "\t \t " \
49
- + "{:.3f}" .format (recall_micro ) + "\n "
50
- results += "macro" + "\t \t " + "{:.3f}" .format (f1_macro ) + "\t \t " \
51
- + "{:.3f}" .format (precision_macro ) + "\t \t " \
52
- + "{:.3f}" .format (recall_macro ) + "\n "
53
- results += "weighted" + "\t " + "{:.3f}" .format (f1_weighted ) + "\t \t " \
54
- + "{:.3f}" .format (precision_weighted ) + "\t \t " \
55
- + "{:.3f}" .format (recall_weighted ) + "\n "
56
-
57
- else :
53
+ results += (
54
+ "micro"
55
+ + "\t \t "
56
+ + "{:.3f}" .format (f1_micro )
57
+ + "\t \t "
58
+ + "{:.3f}" .format (precision_micro )
59
+ + "\t \t "
60
+ + "{:.3f}" .format (recall_micro )
61
+ + "\n "
62
+ )
63
+ results += (
64
+ "macro"
65
+ + "\t \t "
66
+ + "{:.3f}" .format (f1_macro )
67
+ + "\t \t "
68
+ + "{:.3f}" .format (precision_macro )
69
+ + "\t \t "
70
+ + "{:.3f}" .format (recall_macro )
71
+ + "\n "
72
+ )
73
+ results += (
74
+ "weighted"
75
+ + "\t "
76
+ + "{:.3f}" .format (f1_weighted )
77
+ + "\t \t "
78
+ + "{:.3f}" .format (precision_weighted )
79
+ + "\t \t "
80
+ + "{:.3f}" .format (recall_weighted )
81
+ + "\n "
82
+ )
83
+
84
+ else :
58
85
results = dict ()
59
86
results [Metric .Accuracy ] = accuracy
60
87
results [Metric .F1Micro ] = f1_micro
@@ -66,5 +93,5 @@ def get_labels_from_file(file):
66
93
results [Metric .RecallMicro ] = recall_micro
67
94
results [Metric .RecallMacro ] = recall_macro
68
95
results [Metric .RecallWeighted ] = recall_weighted
69
-
70
- return results
96
+
97
+ return results
0 commit comments