Skip to main content

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

Project description

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, 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')

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.

Installation

pip install 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/mdadda/nerval},
  note={Software available from https://github.com/mdadda/nerval},
  author={Mariangela D'Addato},
  year={2022},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nerval-1.0.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

nerval-1.0.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file nerval-1.0.0.tar.gz.

File metadata

  • Download URL: nerval-1.0.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for nerval-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c0831a94ea986d4c82214182665964a4b411982528bbd4fe6798d7dfe80a5f8e
MD5 ed8d906e935da3f04fbbf7e497f680b8
BLAKE2b-256 f415478e91b22bdf2765408f7fa8ff893650c519b4f5c8faca86c81924596fe3

See more details on using hashes here.

File details

Details for the file nerval-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: nerval-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for nerval-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c8c1bbbbe02c27b6e875621d7833cc4073c3b69354d1d32f1938351e2f4b94b
MD5 6fa95b8886fceaccc4c5c59d4d08dcb4
BLAKE2b-256 4a656716e156c1611c79b9c1e509840741efc04707176c900a41530e9a2fc555

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page