class_docstr: 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.
class_docstr_tok: ["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","."]
docstr: 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.
docstr_tok: ["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","."]
code: def __init__(self, reference, test, sort_by_count=False):
      """
      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.
      """
      if len(reference) != len(test):
          raise ValueError("Lists must have the same length.")
  
      # Get a list of all values.
      if sort_by_count:
          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:
          values = sorted(set(reference + test))
  
      # Construct a value->index dictionary
      indices = {val: i for (i, val) in enumerate(values)}
  
      # Make a confusion matrix table.
      confusion = [[0 for _ in values] for _ in values]
      max_conf = 0  # Maximum confusion
      for w, g in zip(reference, test):
          confusion[indices[w]][indices[g]] += 1
          max_conf = max(max_conf, confusion[indices[w]][indices[g]])
  
      #: A list of all values in ``reference`` or ``test``.
      self._values = values
      #: A dictionary mapping values in ``self._values`` to their indices.
      self._indices = indices
      #: The confusion matrix itself (as a list of lists of counts).
      self._confusion = confusion
      #: The greatest count in ``self._confusion`` (used for printing).
      self._max_conf = max_conf
      #: The total number of values in the confusion matrix.
      self._total = len(reference)
      #: The number of correct (on-diagonal) values in the matrix.
      self._correct = sum(confusion[i][i] for i in range(len(values)))
  
file_path: nltk/nltk/metrics/confusionmatrix.py
imports: from nltk.probability import FreqDist
label_desc: Use image node_id 1 to create a new ConfusionMatrix object  with example: obj = ConfusionMatrix(reference, test, sort_by_count)