import torch


def precision(cm):
    """
    Precision: of all predicted positive samples, what fraction was correct?

        precision = TP / (TP + FP)

    Args:
        cm: Binary confusion matrix like this:
            | TN | FP |
            | FN | TP |

    Returns:
        scalar precision score
    """
    rv = cm.diag() / cm.sum(0)
    rv[torch.isnan(rv)]=0
    return rv[1]