Skip to content

Instantly share code, notes, and snippets.

@terrycojones
Created March 9, 2015 16:41
Show Gist options
  • Save terrycojones/69ad2207f503ce682c0d to your computer and use it in GitHub Desktop.
Save terrycojones/69ad2207f503ce682c0d to your computer and use it in GitHub Desktop.
from collections import Counter, defaultdict
class ConfusionMatrix(object):
def __init__(self, trueLabels, clusterLabels):
self._counts = defaultdict(Counter)
self.allLabels = set(trueLabels + clusterLabels)
for trueLabel, clusterLabel in zip(trueLabels, clusterLabels):
self._counts[trueLabel][clusterLabel] += 1
def __getitem__(self, item):
return self._counts[item[0]][item[1]]
def __setitem__(self, item, value):
self._counts[item[0]][item[1]] = value
@terrycojones
Copy link
Author

Initialization is

>>> x = ConfusionMatrix([0, 0, 1, 1], [1, 1, 1, 2])

With access like:

>>> x[0, 1]
2
>>> x[4, 3] += 8
>>> x[4, 3]
8

and all labels are available:

>>> x.allLabels
{0, 1, 2}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment