Coverage for nltk.metrics.confusionmatrix : 75%
![](keybd_closed.png)
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Natural Language Toolkit: Confusion Matrices # # Copyright (C) 2001-2012 NLTK Project # Author: Edward Loper <edloper@gradient.cis.upenn.edu> # Steven Bird <sb@csse.unimelb.edu.au> # URL: <http://www.nltk.org/> # For license information, see LICENSE.TXT
""" The confusion matrix between a list of reference values and a corresponding list of test values. Entry *[r,t]* of this matrix is a count of the number of times that the reference value *r* corresponds to the test value *t*. E.g.:
>>> from nltk.metrics import ConfusionMatrix >>> ref = 'DET NN VB DET JJ NN NN IN DET NN'.split() >>> test = 'DET VB VB DET NN NN NN IN DET NN'.split() >>> cm = ConfusionMatrix(ref, test) >>> print(cm['NN', 'NN']) 3
Note that the diagonal entries *Ri=Tj* of this matrix corresponds to correct values; and the off-diagonal entries correspond to incorrect values. """
""" Construct a new confusion matrix from a list of reference values and a corresponding list of test values.
:type reference: list :param reference: An ordered list of reference values. :type test: list :param test: A list of values to compare against the corresponding reference values. :raise ValueError: If ``reference`` and ``length`` do not have the same length. """ raise ValueError('Lists must have the same length.')
# Get a list of all values. ref_fdist = FreqDist(reference) test_fdist = FreqDist(test) def key(v): return -(ref_fdist[v]+test_fdist[v]) values = sorted(set(reference+test), key=key) else:
# Construct a value->index dictionary
# Make a confusion matrix table.
#: A list of all values in ``reference`` or ``test``. #: A dictionary mapping values in ``self._values`` to their indices. #: The confusion matrix itself (as a list of lists of counts). #: The greatest count in ``self._confusion`` (used for printing). #: The total number of values in the confusion matrix. #: The number of correct (on-diagonal) values in the matrix.
""" :return: The number of times that value ``li`` was expected and value ``lj`` was given. :rtype: int """
return '<ConfusionMatrix: %s/%s correct>' % (self._correct, self._total)
truncate=None, sort_by_count=False): """ :return: A multi-line string representation of this confusion matrix. :type truncate: int :param truncate: If specified, then only show the specified number of values. Any sorting (e.g., sort_by_count) will be performed before truncation. :param sort_by_count: If true, then sort by the count of each label in the reference data. I.e., labels that occur more frequently in the reference label will be towards the left edge of the matrix, and labels that occur less frequently will be towards the right edge.
@todo: add marginals? """
-sum(self._confusion[self._indices[v]]))
else:
# Construct a format string for row values # Construct a format string for matrix entries entrylen = 6 entry_format = '%5.1f%%' zerostr = ' .' else:
# Write the column values. else:
# Write a dividing line
# Write the entries. s += entry_format % (100.0*confusion[i][j]/self._total) else:
# Write a dividing line
# Write a key
values = self._values str = 'Value key:\n' indexlen = len(repr(len(values)-1)) key_format = ' %'+repr(indexlen)+'d: %s\n' for i in range(len(values)): str += key_format % (i, values[i])
return str
reference = 'DET NN VB DET JJ NN NN IN DET NN'.split() test = 'DET VB VB DET NN NN NN IN DET NN'.split() print('Reference =', reference) print('Test =', test) print('Confusion matrix:') print(ConfusionMatrix(reference, test)) print(ConfusionMatrix(reference, test).pp(sort_by_count=True))
demo() |