Skip to content

ENH fast path for binary confusion matrix #15403

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 5 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
54 changes: 36 additions & 18 deletions sklearn/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,44 @@ def confusion_matrix(y_true, y_pred, labels=None, sample_weight=None,
if np.all([l not in y_true for l in labels]):
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:
sample_weight = np.asarray(sample_weight)

check_consistent_length(y_true, y_pred, sample_weight)

if normalize not in ['true', 'pred', 'all', None]:
raise ValueError("normalize must be one of {'true', 'pred', "
"'all', None}")

def norm(cm):
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

if sample_weight is not None:
sample_weight = np.asarray(sample_weight)
check_consistent_length(y_true, y_pred, sample_weight)
Comment on lines +295 to +296
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sample_weight = np.asarray(sample_weight)
check_consistent_length(y_true, y_pred, sample_weight)
sample_weight = _check_sample_weight(sample_weight, X)


if y_type == 'binary' and len(labels) == 2:
# fast path for binary case
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 'iub':
out = out.astype(np.int64)
return norm(out)

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

n_labels = labels.size
label_to_ind = {y: x for x, y in enumerate(labels)}
# convert yt, yp into index
Expand All @@ -301,7 +328,7 @@ def confusion_matrix(y_true, y_pred, labels=None, sample_weight=None,
sample_weight = sample_weight[ind]

# Choose the accumulator dtype to always have high precision
if sample_weight.dtype.kind in {'i', 'u', 'b'}:
if sample_weight.dtype.kind in 'iub':
dtype = np.int64
else:
dtype = np.float64
Expand All @@ -310,16 +337,7 @@ def confusion_matrix(y_true, y_pred, labels=None, sample_weight=None,
shape=(n_labels, n_labels), 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)

return cm
return norm(cm)


def multilabel_confusion_matrix(y_true, y_pred, sample_weight=None,
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 @@ -936,12 +936,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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a neater solution than removing this, but it turns out that different implementations will give different results in the case of overflow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it just test for the binary type and exact selection conditions as in the optimized code before making the assertions to exclude them?

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


def test_classification_report_multiclass():
# Test performance report
Expand Down