Skip to main content

A package for computing informedness, markedness and phi score

Project description

metrify

Installation

To install metrify, simply run pip install metrify.

Purpose

The purpose of metrify is to add a few simple functionalities to scikit-learn. In particular, the informedness, markedness and $\phi_\beta$ scores are introduced. Furthermore, some other utilities related to more traditional metrics (e.g. $F_\beta$) are introduced, too. The reason for introducing them (and hence their utility) stems from the following observations.

The main issue with precision, recall and F-score is that none of them makes use of True Negatives (TNs) to assess the model performance. Whilst we are usually interested in the positive class, ensuring to get the negative class right is a sort of sanity check for our model. For example, if we had a highly imbalanced dataset (towards the negative class) we definitely want the negative class to be predicted with high confidence. Our job should be, in theory, to ensure that the model performs well on the minority, i.e. the positive, class.

In practice, suppose we have a model whose predictions yield, in terms of False Positives, False Negatives and True Positives $$TP = 50, FP = 1000, FN = 10$$ Even though we can compute e.g. the F-score given this information, this metric totally neglects if we had, for example, $TN = 100000$ or $TN = 100$ which would indeed make a very big difference when assessing the model performance. This is a signal that we are not really capturing the information about the model. Whilst it is clear that the model is not satisfactory in either case, as we have an abundance of $FP$s, the latter situation is much more worrisome. In fact, although the negative class is the majority, we are basically getting it totally wrong by always predicting the positive one. This is likely indicating that our model has some inherent issue in its construction, e.g. setting a threshold too low or something similar.

To make sure our metrics do take into consideration this case, we need an alternative to precision and recall. One option is to use markedness $M$ and informedness $I$. Formulas are the following: $$M = \frac{TP}{TP + FP} - \frac{FN}{TN+FN} \qquad I = \frac{TP}{TP + FN} - \frac{FP}{TN+FP}$$ Markedness plays the role of precision, and is informative about the role of $FP$s in our model. Conversely, Informedness plays the role of recall, and gauges the importance of $FN$s. However, in either case, the $TN$s do enter the game and help bring these metrics down. In fact, both of these are bounded between -1 and +1, with -1 corresponding to the worst scenario (no TP, and no TN). It is also possible to combine them into something reminiscent of F-score. I called it $\phi$-score. Similarly to the F-score, it can be weighted by a real parameter $\beta$, so as to give more or less importance to either $M$ or $I$. $$\phi_\beta = (1 + \beta^2) \frac{I \cdot M}{\beta^2 M + I}$$

Usage

Informedness, Markedness, $\phi$

Currently metrify only works for a binary classification problem. A sample usage is the following

from metrify import informedness, markedness, phi_beta

# Define a random numpy array of 100 true values
t = np.random.randint(0, 2, 100)

# Define a random numpy array of 100 predicted values
p = np.random.randint(0, 2, 100)

# Compute the metrics
i = informedness(t, p)
m = markedness(t, p)
phi_2 = phi_beta(t, p, beta=2)

$F_\beta$

Given a set of binary ground truths and predictions as probabilities, find the best $F_beta$ and corresponding threshold:

from metrify import find_best_fbeta

# Define a random numpy array of 100 true values
t = np.random.randint(0, 2, 100)

# Define a random numpy array of 100 predicted probabilities
p = np.random.rand(100)

# Get the best F0.5 and the corresponding threshold
f_beta, threshold = find_best_fbeta(t, p, beta=0.5)

New Versions

To create and deploy a new version, the recipe is:

  • Modify the code as suited;
  • Update the version and possibly the dependencies in pyproject.toml;
  • Build the package python -m build;
  • Upload the package python -m twine upload dist/metrify-<VERSION>*

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

metrify-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metrify-0.1.3-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file metrify-0.1.3.tar.gz.

File metadata

  • Download URL: metrify-0.1.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for metrify-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5ee6cca42e42af53115a52f94a4265b5caa18cdfe3984558e2b53b2125835577
MD5 2d56a6b43e507761cc6447d8800fc43e
BLAKE2b-256 5b192ff918bb1e26e967e465253484182d8a3942a635013c367e59a85eed0e7e

See more details on using hashes here.

File details

Details for the file metrify-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: metrify-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for metrify-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 061b9b92765e12c2557e597ce9f0d97ab001e0dfffa9e57608066390f07eb5a4
MD5 d1d6d1cea5c69c7c9e1b4e36176b8052
BLAKE2b-256 1253a9975c2bfd1e7007fc32221c99b48b0cc5f18464e026f022958b62df767b

See more details on using hashes here.

Supported by

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