Imbalanced binary classification
Evaluate a binary classifier when positive and negative classes have very different sizes.
Scikit-Learn · Function
Computes the Matthews correlation coefficient (MCC) for binary or multiclass classification. The score ranges from -1 for inverse prediction to +1 for perfect prediction, with 0 representing average random prediction.
The function compares ground-truth labels with classifier predictions and returns a correlation-style classification score. It supports binary and multiclass targets, incorporates sample weights when supplied, and ranges from -1 for inverse prediction through 0 for random performance to +1 for perfect prediction.
sklearn.metrics.matthews_corrcoef(y_true, y_pred, *, sample_weight=None)| Name | Type | Description |
|---|---|---|
y_true | array-like of shape (n_samples) | Ground truth (correct) target values. |
y_pred | array-like of shape (n_samples) | Estimated targets as returned by a classifier. |
sample_weight | array-like of shape (n_samples), default=None | Sample weights. |
float — The Matthews correlation coefficient.
from sklearn.metrics import matthews_corrcoef
y_true = [1, 1, 1, -1]
y_pred = [1, -1, 1, 1]
score = matthews_corrcoef(y_true, y_pred)
print(score) # -0.33This computes the MCC for four binary predictions and prints the resulting score.
Evaluate a binary classifier when positive and negative classes have very different sizes.
Compare predicted and true labels across more than two classes.
Give selected observations greater or lesser influence during scoring.
It returns the Matthews correlation coefficient, a balanced classification-quality measure that accounts for prediction errors and is useful when class sizes differ. Its range is -1 to +1: +1 is perfect prediction, 0 is average random prediction, and -1 is inverse prediction.
Follow-up: What does the binary formula use? It combines true positives, true negatives, false positives, and false negatives as (tp tn - fp fn) divided by sqrt((tp+fp)(tp+fn)(tn+fp)(tn+fn)).
Pass y_true as the correct target values and y_pred as the estimated targets returned by a classifier. Both are array-like with shape (n_samples), and sample_weight can optionally assign a weight to each sample.
Follow-up: Why might sample weights matter? They let evaluation give different importance to observations, which is useful when examples have unequal relevance or representation.
The function supports both binary and multiclass labels. In the binary case, the score is directly connected to the four confusion-matrix counts; the documentation does not restrict the function to binary targets.
Follow-up: How should a multiclass score be interpreted? Treat it as the documented multiclass MCC rather than attempting to read it as a binary true-positive/negative statistic.
confusion_matrixbalanced_accuracy_scoref1_scoreprecision_scorerecall_score