mindformers.core.EntityScore

View Source On Gitee
class mindformers.core.EntityScore[source]

Evaluates the precision, recall, and F1 score of predicted entities against the ground truth.

Mathematically, these metrics are defined as follows:

Precision: Measures the fraction of correctly predicted entities out of all predicted entities.

\[\text{Precision} = \frac{\text{Number of correct entities}}{\text{Number of predicted entities}}\]

Recall: Measures the fraction of correctly predicted entities out of all actual entities.

\[\text{Recall} = \frac{\text{Number of correct entities}}{\text{Number of actual entities}}\]

F1 Score: The harmonic mean of precision and recall, providing a balance between them.

\[\text{F1} = \frac{2 \times \text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}\]

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindformers.core.metric.metric import EntityScore
>>> x = Tensor(np.array([[np.arange(0, 22)]]))
>>> y = Tensor(np.array([[21]]))
>>> metric = EntityScore()
>>> metric.clear()
>>> metric.update(x, y)
>>> result = metric.eval()
>>> print(result)
({'precision': 1.0, 'recall': 1.0, 'f1': 1.0}, {'address': {'precision': 1.0, 'recall': 1.0, 'f1': 1.0}})