Datasets:
Tasks:
Token Classification
Modalities:
Text
Formats:
parquet
Sub-tasks:
named-entity-recognition
Languages:
English
Size:
1K - 10K
License:
removed huggingface req
Browse files- scoring-scripts/compute_MCC.py +23 -12
scoring-scripts/compute_MCC.py
CHANGED
@@ -1,19 +1,30 @@
|
|
1 |
from sklearn.metrics import matthews_corrcoef
|
2 |
import numpy as np
|
3 |
-
def
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# check that tokens match
|
11 |
-
|
12 |
-
|
|
|
13 |
# the lists have to be flattened
|
14 |
-
flat_ref_tags = np.concatenate(
|
15 |
-
flat_pred_tags = np.concatenate(
|
16 |
-
|
17 |
mcc_score = matthews_corrcoef(y_true=flat_ref_tags,
|
18 |
y_pred=flat_pred_tags)
|
19 |
|
|
|
1 |
from sklearn.metrics import matthews_corrcoef
|
2 |
import numpy as np
|
3 |
+
def compute_MCC_jsonl(references_jsonl, predictions_jsonl, ref_col='ner_tags', pred_col='pred_ner_tags'):
|
4 |
+
'''
|
5 |
+
Computes the Matthews correlation coeff between two datasets in jsonl format (list of dicts each with same keys).
|
6 |
+
Sorts the datasets by 'unique_id' and verifies that the tokens match.
|
7 |
+
'''
|
8 |
+
# reverse the dict
|
9 |
+
ref_dict = {k:[e[k] for e in references_jsonl] for k in references_jsonl[0].keys()}
|
10 |
+
pred_dict = {k:[e[k] for e in predictions_jsonl] for k in predictions_jsonl[0].keys()}
|
11 |
+
|
12 |
+
# sort by unique_id
|
13 |
+
ref_idx = np.argsort(ref_dict['unique_id'])
|
14 |
+
pred_idx = np.argsort(pred_dict['unique_id'])
|
15 |
+
ref_ner_tags = np.array(ref_dict[ref_col], dtype=object)[ref_idx]
|
16 |
+
pred_ner_tags = np.array(pred_dict[pred_col], dtype=object)[pred_idx]
|
17 |
+
ref_tokens = np.array(ref_dict['tokens'], dtype=object)[ref_idx]
|
18 |
+
pred_tokens = np.array(pred_dict['tokens'], dtype=object)[pred_idx]
|
19 |
+
|
20 |
# check that tokens match
|
21 |
+
for t1,t2 in zip(ref_tokens, pred_tokens):
|
22 |
+
assert(t1==t2)
|
23 |
+
|
24 |
# the lists have to be flattened
|
25 |
+
flat_ref_tags = np.concatenate(ref_ner_tags)
|
26 |
+
flat_pred_tags = np.concatenate(pred_ner_tags)
|
27 |
+
|
28 |
mcc_score = matthews_corrcoef(y_true=flat_ref_tags,
|
29 |
y_pred=flat_pred_tags)
|
30 |
|