#323Matthews Correlation Coefficient (MCC)Easy

Matthews Correlation Coefficient (MCC)

Background

MCC is a single balanced scalar for binary classification quality. Unlike accuracy or F1, it works for any class ratio and gives a clean [1,1][-1, 1] interpretation (1 perfect, 0 random, -1 anti-correlated). Production-grade for imbalanced fraud / medical screening.

Problem statement

Implement mcc(y_true, y_pred):

MCC  =  TPTNFPFN(TP+FP)(TP+FN)(TN+FP)(TN+FN)\text{MCC} \;=\; \frac{TP\cdot TN - FP\cdot FN}{\sqrt{(TP+FP)(TP+FN)(TN+FP)(TN+FN)}}

Input

  • y_true, y_pred - sequences of {0,1}\{0, 1\}, same length.

Output

  • float in [1,1][-1, 1]. Returns 00 if any factor in the denominator is 00 (a degenerate case).

Examples

Input: y_true=[1,1,0,0], y_pred=[1,1,0,0]
Output: 1.0

Constraints

  • Same length, ValueError otherwise.
  • Both inputs binary in {0,1}\{0, 1\}.

Notes

  • vs F1. F1 weights precision and recall, but ignores TN. MCC uses all four cells of the confusion matrix and is therefore symmetric under label flip.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference perfect classifier example
  • Worked example on a balanced mix
  • Sample eight-item confusion