Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

matthews_corrcoef

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.

How it works

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.

Syntax

sklearn.metrics.matthews_corrcoef(y_true, y_pred, *, sample_weight=None)

Parameters

NameTypeDescription
y_truearray-like of shape (n_samples)Ground truth (correct) target values.
y_predarray-like of shape (n_samples)Estimated targets as returned by a classifier.
sample_weightarray-like of shape (n_samples), default=NoneSample weights.

Returns

float — The Matthews correlation coefficient.

Key points

  • Use MCC to evaluate both binary and multiclass classifier predictions.
  • The metric incorporates the main prediction-error categories and remains balanced when class distributions are very uneven.
  • Interpret the result as a bounded correlation-like value from -1 to +1.
  • A score of +1 means perfect predictions, 0 indicates average random performance, and -1 indicates inverse predictions.
  • Both binary and multiclass target-label formats are accepted.

Common mistakes

  • Interpretations based directly on true and false positives and negatives apply only to binary classification; multiclass use is supported but has a different interpretation.

Example: Compute a binary MCC

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.33

This computes the MCC for four binary predictions and prints the resulting score.

When to use it

Imbalanced binary classification

Evaluate a binary classifier when positive and negative classes have very different sizes.

Multiclass evaluation

Compare predicted and true labels across more than two classes.

Weighted evaluation

Give selected observations greater or lesser influence during scoring.

Interview questions

QWhat does matthews_corrcoef measure, and how do you interpret its range?

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)).

QWhat are the function's parameters?

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.

QDoes the function support multiclass classification?

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_matrix
  • balanced_accuracy_score
  • f1_score
  • precision_score
  • recall_score

Source: matthews_corrcoef — scikit-learn 1.9.1 documentation. Checked 2026-10-15.