Skip to content

BinaryPrecision metric

Bases: BinaryFBetaScore

Computes precision on binary structures.

Mirrors BinaryF1Score. Each field of y_true and y_pred should be a boolean or a float in [0, 1]; floats are thresholded against threshold. Per-field precision is TP / (TP + FP), aggregated via average.

Example:

program.compile(
    metrics=[
        synalinks.metrics.BinaryPrecision(),
    ],
)

Parameters:

Name Type Description Default
average str

One of None, "micro", "macro", "weighted".

None
threshold float

Threshold for deciding whether a float value is 1 or 0. Defaults to 0.5.

0.5
name str

(Optional) string name of the metric instance.

'binary_precision'
in_mask list

(Optional) list of keys to keep to compute the metric.

None
out_mask list

(Optional) list of keys to remove to compute the metric.

None
in_mask_pattern str

(Optional) Regex pattern.

None
out_mask_pattern str

(Optional) Regex pattern.

None
Source code in synalinks/src/metrics/precision_recall_metrics.py
@synalinks_export("synalinks.metrics.BinaryPrecision")
class BinaryPrecision(BinaryFBetaScore):
    """Computes precision on binary structures.

    Mirrors `BinaryF1Score`. Each field of `y_true` and `y_pred` should be
    a boolean or a float in `[0, 1]`; floats are thresholded against
    `threshold`. Per-field precision is `TP / (TP + FP)`, aggregated via
    `average`.


    Example:

    ```python
    program.compile(
        metrics=[
            synalinks.metrics.BinaryPrecision(),
        ],
    )
    ```

    Args:
        average (str): One of `None`, `"micro"`, `"macro"`, `"weighted"`.
        threshold (float): Threshold for deciding whether a float value is
            `1` or `0`. Defaults to `0.5`.
        name (str): (Optional) string name of the metric instance.
        in_mask (list): (Optional) list of keys to keep to compute the metric.
        out_mask (list): (Optional) list of keys to remove to compute the metric.
        in_mask_pattern (str): (Optional) Regex pattern.
        out_mask_pattern (str): (Optional) Regex pattern.
    """

    def __init__(
        self,
        average=None,
        threshold=0.5,
        name="binary_precision",
        in_mask=None,
        out_mask=None,
        in_mask_pattern=None,
        out_mask_pattern=None,
    ):
        super().__init__(
            average=average,
            beta=1.0,
            threshold=threshold,
            name=name,
            in_mask=in_mask,
            out_mask=out_mask,
            in_mask_pattern=in_mask_pattern,
            out_mask_pattern=out_mask_pattern,
        )
        self._formula = "precision"

    def get_config(self):
        base_config = super().get_config()
        del base_config["beta"]
        return base_config