Skip to main content

nerval

Entity-level confusion matrix and classification report to evaluate Named Entity Recognition (NER) models.

Labelling schemes supported:

  • IO
  • BIO1 (IOB1)
  • BIO2 (IOB2)
  • IOE1
  • IOE2
  • IOBES
  • BILOU
  • BMEWO

Options for the 'scheme' argument:

  • BIO for the following schemes: IO / BIO1 (IOB1) / BIO2 (IOB2) / IOBES / BILOU / BMEWO
  • IOE for the following schemes: IOE1 / IOE2
  • BIO is the default scheme.

Output:

  • Classification report
  • Confusion matrix
  • Labels for the confusion matrix
  • Confusion matrix plot

How to use it:

>>> from nerval import crm

>>> y_true = [['O', 'B-PER', 'I-PER', 'O', 'O', 'O', 'O', 'B-LOC', 'I-LOC']]
>>> y_pred = [['O', 'B-PER', 'O', 'O', 'O', 'O', 'O', 'O', 'B-LOC']]

>>> cr, cm, cm_labels = crm(y_true, y_pred, scheme='BIO')
True Entities: 2
Pred Entities: 2

True Entities with 3 or more tags: 0
Pred Entities with 3 or more tags: 0

True positives:  0
False positives (true = 'O'):  1
False positives (true <> pred):  1
ToT False positives:  2
False negatives:  1

>>> print(cr)
precision  recall  f1_score  true_entities  pred_entities
PER                0.00    0.00      0.00           1.00           0.00
LOC                0.00    0.00      0.00           1.00           1.00
PER__              0.00    0.00      0.00           0.00           1.00
micro_avg          0.00    0.00      0.00           2.00           2.00
macro_avg          0.00    0.00      0.00           2.00           2.00
weighted_avg       0.00    0.00      0.00           2.00           2.00

>>> print(cm)
[[0 1 0 0]
 [1 0 0 0]
 [0 0 0 1]
 [0 0 0 0]]

>>> print(cm_labels)
['LOC', 'O', 'PER', 'PER__']
>>> from nerval import plot_confusion_matrix

>>> y_true = [['O', 'B-PER', 'I-PER', 'O', 'O', 'O', 'O', 'B-LOC', 'I-LOC']]
>>> y_pred = [['O', 'B-PER', 'O', 'O', 'O', 'O', 'O', 'O', 'B-LOC']]

>>> plot_confusion_matrix(cm, cm_labels, show=True, save=False, img_path=None, normalize=None, decimal_places=2, figsize=(15,15), SMALL_SIZE=8, MEDIUM_SIZE=12, BIGGER_SIZE=14, cmap='OrRd', xticks_rotation='vertical', title='Confusion Matrix')

>>> plot_confusion_matrix(cm, cm_labels, show=True, save=True, img_path=None)

>>> plot_confusion_matrix(cm, cm_labels, show=True, save=True, img_path=r'C:\Users\...\my_conf_matrix.png')

>>> plot_confusion_matrix(cm, cm_labels, show=False, save=True, img_path=None)

>>> plot_confusion_matrix(cm, cm_labels, show=False, save=True, img_path=r'C:\Users\...\my_conf_matrix.png')

Note 1:

y_true and y_pred could be:

  • flat lists
  • lists of flat lists
  • lists of nested lists.

Flat lists and lists of nested lists will be converted to lists of lists.

Note 2:

The __ at the end of some entities means that true and pred have the same entity name for the first token but the prediction is somewhat different from the true label. Examples:

>>> y_true = ['B-ORG', 'I-ORG', 'I-ORG']
>>> y_pred = ['B-ORG']

>>> y_true = ['B-ORG', 'I-ORG', 'I-ORG']
>>> y_pred = ['B-ORG', 'I-ORG', 'I-ORG', 'I-ORG', 'I-ORG']

>>> y_true = ['B-ORG', 'I-ORG', 'I-ORG']
>>> y_pred = ['B-ORG', 'I-PER']

>>> y_true = ['B-ORG', 'I-ORG', 'I-ORG']
>>> y_pred = ['I-ORG', 'I-PER']

Note 3:

The normalize argument could be:

  • None
  • 'true'
  • 'pred'
  • 'all'

Default is None.

Note 4:

In case of division by zero, the result will default to zero.

Note 5:

Parameters in function plot_confusion_matrix():

  • show: show the plot (default: True)
  • save: save the plot (default: False)
  • img_path: where to save the plot - e.g. r'C:\Users\User...\my_conf_matrix.png' (default: None - this means save the plot in current dir)

Installation

pip install nerval
conda install -c conda-forge nerval

License

MIT

Citation

@misc{nerval,
  title={{nerval}: Entity-level confusion matrix and classification report to evaluate Named Entity Recognition (NER) models.},
  url={https://github.com/maridda/nerval},
  note={Software available from https://github.com/maridda/nerval},
  author={Mariangela D'Addato},
  year={2022},
}

Metadata

Release files for nerval 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nerval 1.1.2
File Size Uploaded
nerval-1.1.2.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nerval 1.1.2
File Interpreter ABI Platform
nerval-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 23.0 kB

Release files / nerval-1.1.2.tar.gz

Download URL nerval-1.1.2.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f296ed52e7b9d501a00f92cfb5daecfcf67d493d190f04d54b3d72cfccf23b69
BLAKE2b-256 checksum
How to use checksums
61e8253a210d196d0cce09a8b96b7474549f117e362256f08b54430b036848b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / nerval-1.1.2-py3-none-any.whl

Download URL nerval-1.1.2-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0675573b5be2357b4eb394880474c4de6efb477e502df5d59341b42bff80f5a1
BLAKE2b-256 checksum
How to use checksums
d6ed33b090e5d8dd597ffd070687b73b94df6d1f2ea9be0c7ce52c685b93383a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page