Datasets:

Modalities:
Text
Formats:
parquet
Languages:
English
Libraries:
Datasets
pandas
License:
fgrezes commited on
Commit
6f2dad6
1 Parent(s): 8d91945

removed huggingface req

Browse files
Files changed (1) hide show
  1. 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 compute_MCC(references_dataset, predictions_dataset, ref_col='ner_tags', pred_col='pred_ner_tags'):
4
- # computes the Matthews correlation coeff between two datasets
5
-
6
- # sort by id
7
- references_dataset = references_dataset.sort('unique_id')
8
- predictions_dataset = predictions_dataset.sort('unique_id')
9
-
 
 
 
 
 
 
 
 
 
 
10
  # check that tokens match
11
- assert(references_dataset['tokens']==predictions_dataset['tokens'])
12
-
 
13
  # the lists have to be flattened
14
- flat_ref_tags = np.concatenate(references_dataset[ref_col])
15
- flat_pred_tags = np.concatenate(predictions_dataset[pred_col])
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