weka.classifiers
Class CostMatrix

java.lang.Object
  extended byweka.core.Matrix
      extended byweka.classifiers.CostMatrix
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

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 $
Author:
Richard Kirkby (rkirkby@cs.waikato.ac.nz)
See Also:
Serialized Form

Field Summary
static java.lang.String FILE_EXTENSION
          The deafult file extension for cost matrix files
 
Fields inherited from class weka.core.Matrix
m_Elements
 
Constructor Summary
CostMatrix(CostMatrix toCopy)
          Creates a cost matrix that is a copy of another.
CostMatrix(int numOfClasses)
          Creates a default cost matrix of a particular size.
CostMatrix(java.io.Reader reader)
          Creates a cost matrix from a reader.
 
Method Summary
 Instances applyCostMatrix(Instances data, java.util.Random random)
          Applies the cost matrix to a set of instances.
 double[] expectedCosts(double[] classProbs)
          Calculates the expected misclassification cost for each possible class value, given class probability estimates.
 double getMaxCost(int classVal)
          Gets the maximum cost for a particular class value.
 void initialize()
          Sets the cost of all correct classifications to 0, and all misclassifications to 1.
 void normalize()
          Normalizes the matrix so that the diagonal contains zeros.
 void readOldFormat(java.io.Reader reader)
          Loads a cost matrix in the old format from a reader.
 int size()
          Gets the size of the matrix.
 
Methods inherited from class weka.core.Matrix
add, addElement, clone, eigenvalueDecomposition, getColumn, getElement, getL, getRow, getU, hypot, isSymmetric, LUDecomposition, main, multiply, numColumns, numRows, regression, regression, setColumn, setElement, setRow, solve, testEigen, toString, transpose, write
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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.

Overrides:
initialize in class Matrix

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.