Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 2 contributors
Commits on Jun 03, 2012
@bwhite bwhite Faster confusion_matrix implementation
Signed-off-by: Brandyn A. White <bwhite@dappervision.com>
dc01ed3
@amueller amueller ENH added old confusion_matrix implementation as alternative for few …
…labels.
b6e38ce
Showing with 11 additions and 5 deletions.
  1. +11 −5 sklearn/metrics/metrics.py
View
16 sklearn/metrics/metrics.py
@@ -63,12 +63,18 @@ def confusion_matrix(y_true, y_pred, labels=None):
labels = np.asarray(labels, dtype=np.int)
n_labels = labels.size
+ label_to_ind = dict((y, x) for x, y in enumerate(labels))
- CM = np.empty((n_labels, n_labels), dtype=np.long)
- for i, label_i in enumerate(labels):
- for j, label_j in enumerate(labels):
- CM[i, j] = np.sum(
- np.logical_and(y_true == label_i, y_pred == label_j))
+ if n_labels >= 15:
+ CM = np.zeros((n_labels, n_labels), dtype=np.long)
+ for yt, yp in zip(y_true, y_pred):
+ CM[label_to_ind[yt], label_to_ind[yp]] += 1
+ else:
+ CM = np.empty((n_labels, n_labels), dtype=np.long)
+ for i, label_i in enumerate(labels):
+ for j, label_j in enumerate(labels):
+ CM[i, j] = np.sum(
+ np.logical_and(y_true == label_i, y_pred == label_j))
return CM

No commit comments for this range

Something went wrong with that request. Please try again.