Skip to content

ENH Add fast path for binary confusion matrix #28578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions sklearn/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ def accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None):
return _weighted_sum(score, sample_weight, normalize)


def _norm_cm(cm, normalize):
"""Normalize confusion matrix."""
with np.errstate(all="ignore"):
if normalize == "true":
cm = cm / cm.sum(axis=1, keepdims=True)
elif normalize == "pred":
cm = cm / cm.sum(axis=0, keepdims=True)
elif normalize == "all":
cm = cm / cm.sum()
cm = np.nan_to_num(cm)
return cm


@validate_params(
{
"y_true": ["array-like"],
Expand Down Expand Up @@ -338,12 +351,28 @@ def confusion_matrix(
elif len(np.intersect1d(y_true, labels)) == 0:
raise ValueError("At least one label specified must be in y_true")

if sample_weight is None:
sample_weight = np.ones(y_true.shape[0], dtype=np.int64)
else:
if sample_weight is not None:
sample_weight = np.asarray(sample_weight)
check_consistent_length(y_true, y_pred, sample_weight)

# fast path for binary case
if y_type == "binary" and len(labels) == 2:
if labels[0] != 0 or labels[1] != 1:
y_true = y_true == labels[1]
y_pred = y_pred == labels[1]

# bincount does not handle 'object' sample_weight
if sample_weight is not None and sample_weight.dtype.kind == "O":
sample_weight = sample_weight.astype(float)
out = np.bincount(
y_true * 2 + y_pred, weights=sample_weight, minlength=4
).reshape(2, 2)
if sample_weight is None or sample_weight.dtype.kind in {"i", "u", "b"}:
out = out.astype(np.int64)
return _norm_cm(out, normalize)

check_consistent_length(y_true, y_pred, sample_weight)
if sample_weight is None:
sample_weight = np.ones(y_true.shape[0], dtype=np.int64)

n_labels = labels.size
# If labels are not consecutive integers starting from zero, then
Expand Down Expand Up @@ -379,15 +408,6 @@ def confusion_matrix(
dtype=dtype,
).toarray()

with np.errstate(all="ignore"):
if normalize == "true":
cm = cm / cm.sum(axis=1, keepdims=True)
elif normalize == "pred":
cm = cm / cm.sum(axis=0, keepdims=True)
elif normalize == "all":
cm = cm / cm.sum()
cm = np.nan_to_num(cm)

if cm.shape == (1, 1):
warnings.warn(
(
Expand All @@ -398,7 +418,7 @@ def confusion_matrix(
UserWarning,
)

return cm
return _norm_cm(cm, normalize)


@validate_params(
Expand Down
6 changes: 0 additions & 6 deletions sklearn/metrics/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,12 +1166,6 @@ def test_confusion_matrix_dtype():
assert cm[0, 0] == 4294967295
assert cm[1, 1] == 8589934590

# np.iinfo(np.int64).max should cause an overflow
weight = np.full(len(y), 9223372036854775807, dtype=np.int64)
cm = confusion_matrix(y, y, sample_weight=weight)
assert cm[0, 0] == 9223372036854775807
assert cm[1, 1] == -2


@pytest.mark.parametrize("dtype", ["Int64", "Float64", "boolean"])
def test_confusion_matrix_pandas_nullable(dtype):
Expand Down