Class CostMatrix

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class CostMatrix
    extends Matrix
    Class for storing and manipulating a misclassification cost matrix. The element at position i,j in the matrix is the penalty for classifying an instance of class j as class i.
    Version:
    $Revision: 1.9.2.1 $
    Author:
    Richard Kirkby (rkirkby@cs.waikato.ac.nz)
    See Also:
    Serialized Form
    • Field Detail

      • FILE_EXTENSION

        public static java.lang.String FILE_EXTENSION
        The deafult file extension for cost matrix files
    • Constructor Detail

      • CostMatrix

        public CostMatrix​(CostMatrix toCopy)
        Creates a cost matrix that is a copy of another.
        Parameters:
        toCopy - the matrix to copy.
      • CostMatrix

        public CostMatrix​(int numOfClasses)
        Creates a default cost matrix of a particular size. All values will be 0.
        Parameters:
        numOfClasses - the number of classes that the cost matrix holds.
      • CostMatrix

        public CostMatrix​(java.io.Reader reader)
                   throws java.lang.Exception
        Creates a cost matrix from a reader.
        Parameters:
        reader - the reader to get the values from.
        Throws:
        java.lang.Exception - if the matrix is invalid.
    • Method Detail

      • initialize

        public void initialize()
        Sets the cost of all correct classifications to 0, and all misclassifications to 1.
      • size

        public int size()
        Gets the size of the matrix.
        Returns:
        the size.
      • applyCostMatrix

        public Instances applyCostMatrix​(Instances data,
                                         java.util.Random random)
                                  throws java.lang.Exception
        Applies the cost matrix to a set of instances. If a random number generator is supplied the instances will be resampled, otherwise they will be rewighted. Adapted from code once sitting in Instances.java
        Parameters:
        data - the instances to reweight.
        random - a random number generator for resampling, if null then instances are rewighted.
        Returns:
        a new dataset reflecting the cost of misclassification.
        Throws:
        java.lang.Exception - if the data has no class or the matrix in inappropriate.
      • expectedCosts

        public double[] expectedCosts​(double[] classProbs)
                               throws java.lang.Exception
        Calculates the expected misclassification cost for each possible class value, given class probability estimates.
        Parameters:
        classProbs - the class probability estimates.
        Returns:
        the expected costs.
        Throws:
        java.lang.Exception - if the wrong number of class probabilities is supplied.
      • getMaxCost

        public double getMaxCost​(int classVal)
        Gets the maximum cost for a particular class value.
        Parameters:
        classVal - the class value.
        Returns:
        the maximum cost.
      • normalize

        public void normalize()
        Normalizes the matrix so that the diagonal contains zeros.
      • readOldFormat

        public void readOldFormat​(java.io.Reader reader)
                           throws java.lang.Exception
        Loads a cost matrix in the old format from a reader. Adapted from code once sitting in Instances.java
        Parameters:
        reader - the reader to get the values from.
        Throws:
        java.lang.Exception - if the matrix cannot be read correctly.