nltk.metrics.confusionmatrix module

class nltk.metrics.confusionmatrix.ConfusionMatrix[source]

Bases: object

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.

__init__(reference, test, sort_by_count=False)[source]

Construct a new confusion matrix from a list of reference values and a corresponding list of test values.

Parameters
  • reference (list) – An ordered list of reference values.

  • test (list) – A list of values to compare against the corresponding reference values.

Raises

ValueError – If reference and length do not have the same length.

pretty_format(show_percents=False, values_in_chart=True, truncate=None, sort_by_count=False)[source]
Returns

A multi-line string representation of this confusion matrix.

Parameters
  • truncate (int) – If specified, then only show the specified number of values. Any sorting (e.g., sort_by_count) will be performed before truncation.

  • 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?

key()[source]
nltk.metrics.confusionmatrix.demo()[source]