Class CostSensitiveClassifier

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, CapabilitiesHandler, Drawable, OptionHandler, Randomizable, RevisionHandler

    public class CostSensitiveClassifier
    extends RandomizableSingleClassifierEnhancer
    implements OptionHandler, Drawable
    A metaclassifier that makes its base classifier cost-sensitive. Two methods can be used to introduce cost-sensitivity: reweighting training instances according to the total cost assigned to each class; or predicting the class with minimum expected misclassification cost (rather than the most likely class). Performance can often be improved by using a Bagged classifier to improve the probability estimates of the base classifier.

    Valid options are:

     -M
      Minimize expected misclassification cost. Default is to
      reweight training instances according to costs per class
     -C <cost file name>
      File name of a cost matrix to use. If this is not supplied,
      a cost matrix will be loaded on demand. The name of the
      on-demand file is the relation name of the training data
      plus ".cost", and the path to the on-demand file is
      specified with the -N option.
     -N <directory>
      Name of a directory to search for cost files when loading
      costs on demand (default current directory).
     -cost-matrix <matrix>
      The cost matrix in Matlab single line format.
     -S <num>
      Random number seed.
      (default 1)
     -D
      If set, classifier is run in debug mode and
      may output additional info to the console
     -W
      Full name of base classifier.
      (default: weka.classifiers.rules.ZeroR)
     
     Options specific to classifier weka.classifiers.rules.ZeroR:
     
     -D
      If set, classifier is run in debug mode and
      may output additional info to the console
    Options after -- are passed to the designated classifier.

    Version:
    $Revision: 1.29 $
    Author:
    Len Trigg (len@reeltwo.com)
    See Also:
    Serialized Form
    • Field Detail

      • MATRIX_ON_DEMAND

        public static final int MATRIX_ON_DEMAND
        load cost matrix on demand
        See Also:
        Constant Field Values
      • MATRIX_SUPPLIED

        public static final int MATRIX_SUPPLIED
        use explicit cost matrix
        See Also:
        Constant Field Values
      • TAGS_MATRIX_SOURCE

        public static final Tag[] TAGS_MATRIX_SOURCE
        Specify possible sources of the cost matrix
    • Constructor Detail

      • CostSensitiveClassifier

        public CostSensitiveClassifier()
        Default constructor.
    • Method Detail

      • setOptions

        public void setOptions​(java.lang.String[] options)
                        throws java.lang.Exception
        Parses a given list of options.

        Valid options are:

         -M
          Minimize expected misclassification cost. Default is to
          reweight training instances according to costs per class
         -C <cost file name>
          File name of a cost matrix to use. If this is not supplied,
          a cost matrix will be loaded on demand. The name of the
          on-demand file is the relation name of the training data
          plus ".cost", and the path to the on-demand file is
          specified with the -N option.
         -N <directory>
          Name of a directory to search for cost files when loading
          costs on demand (default current directory).
         -cost-matrix <matrix>
          The cost matrix in Matlab single line format.
         -S <num>
          Random number seed.
          (default 1)
         -D
          If set, classifier is run in debug mode and
          may output additional info to the console
         -W
          Full name of base classifier.
          (default: weka.classifiers.rules.ZeroR)
         
         Options specific to classifier weka.classifiers.rules.ZeroR:
         
         -D
          If set, classifier is run in debug mode and
          may output additional info to the console
        Options after -- are passed to the designated classifier.

        Specified by:
        setOptions in interface OptionHandler
        Overrides:
        setOptions in class RandomizableSingleClassifierEnhancer
        Parameters:
        options - the list of options as an array of strings
        Throws:
        java.lang.Exception - if an option is not supported
      • globalInfo

        public java.lang.String globalInfo()
        Returns:
        a description of the classifier suitable for displaying in the explorer/experimenter gui
      • costMatrixSourceTipText

        public java.lang.String costMatrixSourceTipText()
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • getCostMatrixSource

        public SelectedTag getCostMatrixSource()
        Gets the source location method of the cost matrix. Will be one of MATRIX_ON_DEMAND or MATRIX_SUPPLIED.
        Returns:
        the cost matrix source.
      • setCostMatrixSource

        public void setCostMatrixSource​(SelectedTag newMethod)
        Sets the source location of the cost matrix. Values other than MATRIX_ON_DEMAND or MATRIX_SUPPLIED will be ignored.
        Parameters:
        newMethod - the cost matrix location method.
      • onDemandDirectoryTipText

        public java.lang.String onDemandDirectoryTipText()
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • getOnDemandDirectory

        public java.io.File getOnDemandDirectory()
        Returns the directory that will be searched for cost files when loading on demand.
        Returns:
        The cost file search directory.
      • setOnDemandDirectory

        public void setOnDemandDirectory​(java.io.File newDir)
        Sets the directory that will be searched for cost files when loading on demand.
        Parameters:
        newDir - The cost file search directory.
      • minimizeExpectedCostTipText

        public java.lang.String minimizeExpectedCostTipText()
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • getMinimizeExpectedCost

        public boolean getMinimizeExpectedCost()
        Gets the value of MinimizeExpectedCost.
        Returns:
        Value of MinimizeExpectedCost.
      • setMinimizeExpectedCost

        public void setMinimizeExpectedCost​(boolean newMinimizeExpectedCost)
        Set the value of MinimizeExpectedCost.
        Parameters:
        newMinimizeExpectedCost - Value to assign to MinimizeExpectedCost.
      • costMatrixTipText

        public java.lang.String costMatrixTipText()
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • getCostMatrix

        public CostMatrix getCostMatrix()
        Gets the misclassification cost matrix.
        Returns:
        the cost matrix
      • setCostMatrix

        public void setCostMatrix​(CostMatrix newCostMatrix)
        Sets the misclassification cost matrix.
        Parameters:
        newCostMatrix - the cost matrix
      • buildClassifier

        public void buildClassifier​(Instances data)
                             throws java.lang.Exception
        Builds the model of the base learner.
        Specified by:
        buildClassifier in class Classifier
        Parameters:
        data - the training data
        Throws:
        java.lang.Exception - if the classifier could not be built successfully
      • distributionForInstance

        public double[] distributionForInstance​(Instance instance)
                                         throws java.lang.Exception
        Returns class probabilities. When minimum expected cost approach is chosen, returns probability one for class with the minimum expected misclassification cost. Otherwise it returns the probability distribution returned by the base classifier.
        Overrides:
        distributionForInstance in class Classifier
        Parameters:
        instance - the instance to be classified
        Returns:
        the computed distribution for the given instance
        Throws:
        java.lang.Exception - if instance could not be classified successfully
      • graphType

        public int graphType()
        Returns the type of graph this classifier represents.
        Specified by:
        graphType in interface Drawable
        Returns:
        the type of graph this classifier represents
      • graph

        public java.lang.String graph()
                               throws java.lang.Exception
        Returns graph describing the classifier (if possible).
        Specified by:
        graph in interface Drawable
        Returns:
        the graph of the classifier in dotty format
        Throws:
        java.lang.Exception - if the classifier cannot be graphed
      • toString

        public java.lang.String toString()
        Output a representation of this classifier
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of the classifier
      • main

        public static void main​(java.lang.String[] argv)
        Main method for testing this class.
        Parameters:
        argv - should contain the following arguments: -t training file [-T test file] [-c class index]