| java.lang.Object weka.classifiers.CostMatrix
CostMatrix | public class CostMatrix implements Serializable(Code) | | 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. Cost values can be fixed or
computed on a per-instance basis (cost sensitive evaluation only)
from the value of an attribute or an expression involving
attribute(s).
author: Mark Hall author: Richard Kirkby (rkirkby@cs.waikato.ac.nz) version: $Revision: 1.15 $ |
Constructor Summary | |
public | CostMatrix(int numOfClasses) Creates a default cost matrix of a particular size. | public | CostMatrix(CostMatrix toCopy) Creates a cost matrix that is a copy of another. | public | CostMatrix(Reader reader) Reads a matrix from a reader. |
Method Summary | |
public Instances | applyCostMatrix(Instances data, Random random) Applies the cost matrix to a set of instances. | public double[] | expectedCosts(double[] classProbs) Calculates the expected misclassification cost for each possible class value,
given class probability estimates. | public double[] | expectedCosts(double[] classProbs, Instance inst) Calculates the expected misclassification cost for each possible class value,
given class probability estimates. | final public Object | getCell(int rowIndex, int columnIndex) Return the contents of a particular cell. | final public double | getElement(int rowIndex, int columnIndex) | final public double | getElement(int rowIndex, int columnIndex, Instance inst) Return the value of a cell as a double. | public double | getMaxCost(int classVal) Gets the maximum cost for a particular class value.
Parameters: classVal - the class value. | public double | getMaxCost(int classVal, Instance inst) Gets the maximum cost for a particular class value.
Parameters: classVal - the class value. | public void | initialize() | public void | normalize() Normalizes the matrix so that the diagonal contains zeros. | public int | numColumns() | public int | numRows() | public static Matrix | parseMatlab(String matlab) creates a matrix from the given Matlab string. | public void | readOldFormat(Reader reader) Loads a cost matrix in the old format from a reader. | final public void | setCell(int rowIndex, int columnIndex, Object value) | final public void | setElement(int rowIndex, int columnIndex, double value) | public int | size() | public 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]. | public String | toString() Converts a matrix to a string. | public void | write(Writer w) Writes out a matrix. |
FILE_EXTENSION | public static String FILE_EXTENSION(Code) | | The deafult file extension for cost matrix files
|
CostMatrix | public CostMatrix(int numOfClasses)(Code) | | Creates a default cost matrix of a particular size.
All diagonal values will be 0 and all non-diagonal values 1.
Parameters: numOfClasses - the number of classes that the cost matrix holds. |
CostMatrix | public CostMatrix(CostMatrix toCopy)(Code) | | Creates a cost matrix that is a copy of another.
Parameters: toCopy - the matrix to copy. |
CostMatrix | public CostMatrix(Reader reader) throws Exception(Code) | | Reads a matrix from a reader. The first line in the file should
contain the number of rows and columns. Subsequent lines
contain elements of the matrix.
(FracPete: taken from old weka.core.Matrix class)
Parameters: reader - the reader containing the matrix throws: Exception - if an error occurs See Also: CostMatrix.write(Writer) |
applyCostMatrix | public Instances applyCostMatrix(Instances data, Random random) throws Exception(Code) | | 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. Parameters: random - a random number generator for resampling, if null then instances arerewighted. a new dataset reflecting the cost of misclassification. exception: Exception - if the data has no class or the matrix in inappropriate. |
expectedCosts | public double[] expectedCosts(double[] classProbs) throws Exception(Code) | | Calculates the expected misclassification cost for each possible class value,
given class probability estimates.
Parameters: classProbs - the class probability estimates. the expected costs. exception: Exception - if the wrong number of class probabilities is supplied. |
expectedCosts | public double[] expectedCosts(double[] classProbs, Instance inst) throws Exception(Code) | | Calculates the expected misclassification cost for each possible class value,
given class probability estimates.
Parameters: classProbs - the class probability estimates. Parameters: inst - the current instance for which the class probabilitesapply. Is used for computing any non-fixed cost values. the expected costs. exception: Exception - if something goes wrong |
getCell | final public Object getCell(int rowIndex, int columnIndex)(Code) | | Return the contents of a particular cell. Note: this
method returns the Object stored at a particular cell.
Parameters: rowIndex - the row Parameters: columnIndex - the column the value at the cell |
getElement | final public double getElement(int rowIndex, int columnIndex) throws Exception(Code) | | Return the value of a cell as a double (for legacy code)
Parameters: rowIndex - the row Parameters: columnIndex - the column the value at a particular cell as a double exception: Exception - if the value is not a double |
getElement | final public double getElement(int rowIndex, int columnIndex, Instance inst) throws Exception(Code) | | Return the value of a cell as a double. Computes the
value for non-fixed costs using the supplied Instance
Parameters: rowIndex - the row Parameters: columnIndex - the column the value from a particular cell exception: Exception - if something goes wrong |
getMaxCost | public double getMaxCost(int classVal) throws Exception(Code) | | Gets the maximum cost for a particular class value.
Parameters: classVal - the class value. the maximum cost. exception: Exception - if cost matrix contains non-fixedcosts |
getMaxCost | public double getMaxCost(int classVal, Instance inst) throws Exception(Code) | | Gets the maximum cost for a particular class value.
Parameters: classVal - the class value. the maximum cost. exception: Exception - if cost matrix contains non-fixedcosts |
initialize | public void initialize()(Code) | | Initializes the matrix
|
normalize | public void normalize()(Code) | | Normalizes the matrix so that the diagonal contains zeros.
|
numColumns | public int numColumns()(Code) | | Same as size
the number of columns |
numRows | public int numRows()(Code) | | Same as size
the number of rows |
parseMatlab | public static Matrix parseMatlab(String matlab) throws Exception(Code) | | creates a matrix from the given Matlab string.
Parameters: matlab - the matrix in matlab format the matrix represented by the given string |
readOldFormat | public void readOldFormat(Reader reader) throws Exception(Code) | | 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. exception: Exception - if the matrix cannot be read correctly. |
setCell | final public void setCell(int rowIndex, int columnIndex, Object value)(Code) | | Set the value of a particular cell in the matrix
Parameters: rowIndex - the row Parameters: columnIndex - the column Parameters: value - the value to set |
setElement | final public void setElement(int rowIndex, int columnIndex, double value)(Code) | | Set the value of a cell as a double
Parameters: rowIndex - the row Parameters: columnIndex - the column Parameters: value - the value (double) to set |
size | public int size()(Code) | | The number of rows (and columns)
the size of the matrix |
toMatlab | public String toMatlab()(Code) | | 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].
the matrix in Matlab single line format |
toString | public String toString()(Code) | | Converts a matrix to a string.
(FracPete: taken from old weka.core.Matrix class)
the converted string |
write | public void write(Writer w) throws Exception(Code) | | Writes out a matrix. The format can be read via the
CostMatrix(Reader) constructor.
(FracPete: taken from old weka.core.Matrix class)
Parameters: w - the output Writer throws: Exception - if an error occurs |
|
|