public class CostMatrix extends Object implements Serializable, RevisionHandler
Modifier and Type | Field and Description |
---|---|
static String |
FILE_EXTENSION
The deafult file extension for cost matrix files
|
Constructor and Description |
---|
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(Reader reader)
Reads a matrix from a reader.
|
Modifier and Type | Method and Description |
---|---|
Instances |
applyCostMatrix(Instances data,
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[] |
expectedCosts(double[] classProbs,
Instance inst)
Calculates the expected misclassification cost for each possible class value,
given class probability estimates.
|
Object |
getCell(int rowIndex,
int columnIndex)
Return the contents of a particular cell.
|
double |
getElement(int rowIndex,
int columnIndex)
Return the value of a cell as a double (for legacy code)
|
double |
getElement(int rowIndex,
int columnIndex,
Instance inst)
Return the value of a cell as a double.
|
double |
getMaxCost(int classVal)
Gets the maximum cost for a particular class value.
|
double |
getMaxCost(int classVal,
Instance inst)
Gets the maximum cost for a particular class value.
|
String |
getRevision()
Returns the revision string.
|
void |
initialize()
Initializes the matrix
|
void |
normalize()
Normalizes the matrix so that the diagonal contains zeros.
|
int |
numColumns()
Same as size
|
int |
numRows()
Same as size
|
static Matrix |
parseMatlab(String matlab)
creates a matrix from the given Matlab string.
|
void |
readOldFormat(Reader reader)
Loads a cost matrix in the old format from a reader.
|
void |
setCell(int rowIndex,
int columnIndex,
Object value)
Set the value of a particular cell in the matrix
|
void |
setElement(int rowIndex,
int columnIndex,
double value)
Set the value of a cell as a double
|
int |
size()
The number of rows (and columns)
|
String |
toMatlab()
converts the Matrix into a single line Matlab string: matrix is enclosed
by parentheses, rows are separated by semicolon and single cells by
blanks, e.g., [1 2; 3 4].
|
String |
toString()
Converts a matrix to a string.
|
void |
write(Writer w)
Writes out a matrix.
|
public static String FILE_EXTENSION
public CostMatrix(int numOfClasses)
numOfClasses
- the number of classes that the cost matrix holds.public CostMatrix(CostMatrix toCopy)
toCopy
- the matrix to copy.public CostMatrix(Reader reader) throws Exception
reader
- the reader containing the matrixException
- if an error occurswrite(Writer)
public void initialize()
public int size()
public int numColumns()
public int numRows()
public Instances applyCostMatrix(Instances data, Random random) throws Exception
data
- the instances to reweight.random
- a random number generator for resampling, if null then instances are
rewighted.Exception
- if the data has no class or the matrix in inappropriate.public double[] expectedCosts(double[] classProbs) throws Exception
classProbs
- the class probability estimates.Exception
- if the wrong number of class probabilities is supplied.public double[] expectedCosts(double[] classProbs, Instance inst) throws Exception
classProbs
- the class probability estimates.inst
- the current instance for which the class probabilites
apply. Is used for computing any non-fixed cost values.Exception
- if something goes wrongpublic double getMaxCost(int classVal) throws Exception
classVal
- the class value.Exception
- if cost matrix contains non-fixed
costspublic double getMaxCost(int classVal, Instance inst) throws Exception
classVal
- the class value.Exception
- if cost matrix contains non-fixed
costspublic void normalize()
public void readOldFormat(Reader reader) throws Exception
reader
- the reader to get the values from.Exception
- if the matrix cannot be read correctly.public void write(Writer w) throws Exception
w
- the output WriterException
- if an error occurspublic String toMatlab()
public final void setCell(int rowIndex, int columnIndex, Object value)
rowIndex
- the rowcolumnIndex
- the columnvalue
- the value to setpublic final Object getCell(int rowIndex, int columnIndex)
rowIndex
- the rowcolumnIndex
- the columnpublic final double getElement(int rowIndex, int columnIndex) throws Exception
rowIndex
- the rowcolumnIndex
- the columnException
- if the value is not a doublepublic final double getElement(int rowIndex, int columnIndex, Instance inst) throws Exception
rowIndex
- the rowcolumnIndex
- the columnException
- if something goes wrongpublic final void setElement(int rowIndex, int columnIndex, double value)
rowIndex
- the rowcolumnIndex
- the columnvalue
- the value (double) to setpublic static Matrix parseMatlab(String matlab) throws Exception
matlab
- the matrix in matlab formatException
public String toString()
public String getRevision()
getRevision
in interface RevisionHandler
Copyright © 2012 University of Waikato, Hamilton, NZ. All Rights Reserved.