public class CostMatrix extends java.lang.Object implements java.io.Serializable, RevisionHandler
UnsupportedAttributeTypeException
,
Serialized FormModifier and Type | Field and Description |
---|---|
static java.lang.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(java.io.Reader reader)
Reads a matrix from a reader.
|
Modifier and Type | Method and Description |
---|---|
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[] |
expectedCosts(double[] classProbs,
Instance inst)
Calculates the expected misclassification cost for each possible class
value, given class probability estimates.
|
java.lang.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.
|
java.lang.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 CostMatrix |
parseMatlab(java.lang.String matlab)
creates a matrix from the given Matlab string.
|
void |
readOldFormat(java.io.Reader reader)
Loads a cost matrix in the old format from a reader.
|
void |
setCell(int rowIndex,
int columnIndex,
java.lang.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)
|
java.lang.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].
|
java.lang.String |
toString()
Converts a matrix to a string.
|
void |
write(java.io.Writer w)
Writes out a matrix.
|
public static java.lang.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(java.io.Reader reader) throws java.lang.Exception
reader
- the reader containing the matrixjava.lang.Exception
- if an error occurswrite(Writer)
public void initialize()
public int size()
public int numColumns()
public int numRows()
public Instances applyCostMatrix(Instances data, java.util.Random random) throws java.lang.Exception
data
- the instances to reweight.random
- a random number generator for resampling, if null then
instances are rewighted.java.lang.Exception
- if the data has no class or the matrix in
inappropriate.public double[] expectedCosts(double[] classProbs) throws java.lang.Exception
classProbs
- the class probability estimates.java.lang.Exception
- if the wrong number of class probabilities is
supplied.public double[] expectedCosts(double[] classProbs, Instance inst) throws java.lang.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.java.lang.Exception
- if something goes wrongpublic double getMaxCost(int classVal) throws java.lang.Exception
classVal
- the class value.java.lang.Exception
- if cost matrix contains non-fixed costspublic double getMaxCost(int classVal, Instance inst) throws java.lang.Exception
classVal
- the class value.java.lang.Exception
- if cost matrix contains non-fixed costspublic void normalize()
public void readOldFormat(java.io.Reader reader) throws java.lang.Exception
reader
- the reader to get the values from.java.lang.Exception
- if the matrix cannot be read correctly.public void write(java.io.Writer w) throws java.lang.Exception
w
- the output Writerjava.lang.Exception
- if an error occurspublic java.lang.String toMatlab()
public static CostMatrix parseMatlab(java.lang.String matlab) throws java.lang.Exception
matlab
- the matrix in matlab formatjava.lang.Exception
toMatlab()
public final void setCell(int rowIndex, int columnIndex, java.lang.Object value)
rowIndex
- the rowcolumnIndex
- the columnvalue
- the value to setpublic final java.lang.Object getCell(int rowIndex, int columnIndex)
rowIndex
- the rowcolumnIndex
- the columnpublic final double getElement(int rowIndex, int columnIndex) throws java.lang.Exception
rowIndex
- the rowcolumnIndex
- the columnjava.lang.Exception
- if the value is not a doublepublic final double getElement(int rowIndex, int columnIndex, Instance inst) throws java.lang.Exception
rowIndex
- the rowcolumnIndex
- the columnjava.lang.Exception
- if something goes wrongpublic final void setElement(int rowIndex, int columnIndex, double value)
rowIndex
- the rowcolumnIndex
- the columnvalue
- the value (double) to setpublic java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getRevision()
getRevision
in interface RevisionHandler