/** * Returns a value at the specified position in the cost matrix. * * @param row the row position * @param column the column position * @return the value */ @Override public Object getValueAt(int row, int column) { // return new Double(m_matrix.getElement(row, column)); try { return m_matrix.getCell(row, column); } catch (Exception ex) { ex.printStackTrace(); } return new Double(0.0); }
/** * Returns a value at the specified position in the cost matrix. * * @param row the row position * @param column the column position * @return the value */ @Override public Object getValueAt(int row, int column) { // return new Double(m_matrix.getElement(row, column)); try { return m_matrix.getCell(row, column); } catch (Exception ex) { ex.printStackTrace(); } return new Double(0.0); }
private boolean replaceStrings(Instances dataset) throws Exception { boolean nonDouble = false; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { if (getCell(i, j) instanceof String) { setCell(i, j, new InstanceExpression((String) getCell(i, j), dataset)); nonDouble = true; } else if (getCell(i, j) instanceof InstanceExpression) { nonDouble = true; } } } return nonDouble; }
private boolean replaceStrings(Instances dataset) throws Exception { boolean nonDouble = false; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { if (getCell(i, j) instanceof String) { setCell(i, j, new InstanceExpression((String) getCell(i, j), dataset)); nonDouble = true; } else if (getCell(i, j) instanceof InstanceExpression) { nonDouble = true; } } } return nonDouble; }
/** * Gets the maximum cost for a particular class value. * * @param classVal the class value. * @return the maximum cost. * @exception Exception if cost matrix contains non-fixed costs */ public double getMaxCost(int classVal) throws Exception { double maxCost = Double.NEGATIVE_INFINITY; for (int i = 0; i < m_size; i++) { Object element = getCell(classVal, i); if (!(element instanceof Double)) { throw new Exception("Can't use non-fixed costs when " + "getting max cost."); } double cost = ((Double) element).doubleValue(); if (cost > maxCost) maxCost = cost; } return maxCost; }
/** * Gets the maximum cost for a particular class value. * * @param classVal the class value. * @return the maximum cost. * @exception Exception if cost matrix contains non-fixed costs */ public double getMaxCost(int classVal) throws Exception { double maxCost = Double.NEGATIVE_INFINITY; for (int i = 0; i < m_size; i++) { Object element = getCell(classVal, i); if (!(element instanceof Double)) { throw new Exception("Can't use non-fixed costs when " + "getting max cost."); } double cost = ((Double) element).doubleValue(); if (cost > maxCost) maxCost = cost; } return maxCost; }
/** * Writes out a matrix. The format can be read via the CostMatrix(Reader) * constructor. (FracPete: taken from old weka.core.Matrix class) * * @param w the output Writer * @throws Exception if an error occurs */ public void write(Writer w) throws Exception { w.write("% Rows\tColumns\n"); w.write("" + m_size + "\t" + m_size + "\n"); w.write("% Matrix elements\n"); for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { w.write("" + getCell(i, j) + "\t"); } w.write("\n"); } w.flush(); }
/** * Writes out a matrix. The format can be read via the CostMatrix(Reader) * constructor. (FracPete: taken from old weka.core.Matrix class) * * @param w the output Writer * @throws Exception if an error occurs */ public void write(Writer w) throws Exception { w.write("% Rows\tColumns\n"); w.write("" + m_size + "\t" + m_size + "\n"); w.write("% Matrix elements\n"); for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { w.write("" + getCell(i, j) + "\t"); } w.write("\n"); } w.flush(); }
/** * Calculates the expected misclassification cost for each possible class * value, given class probability estimates. * * @param classProbs the class probability estimates. * @return the expected costs. * @exception Exception if the wrong number of class probabilities is * supplied. */ public double[] expectedCosts(double[] classProbs) throws Exception { if (classProbs.length != m_size) { throw new Exception("Length of probability estimates don't " + "match cost matrix"); } double[] costs = new double[m_size]; for (int x = 0; x < m_size; x++) { for (int y = 0; y < m_size; y++) { Object element = getCell(y, x); if (!(element instanceof Double)) { throw new Exception("Can't use non-fixed costs in " + "computing expected costs."); } costs[x] += classProbs[y] * ((Double) element).doubleValue(); } } return costs; }
/** * Calculates the expected misclassification cost for each possible class * value, given class probability estimates. * * @param classProbs the class probability estimates. * @return the expected costs. * @exception Exception if the wrong number of class probabilities is * supplied. */ public double[] expectedCosts(double[] classProbs) throws Exception { if (classProbs.length != m_size) { throw new Exception("Length of probability estimates don't " + "match cost matrix"); } double[] costs = new double[m_size]; for (int x = 0; x < m_size; x++) { for (int y = 0; y < m_size; y++) { Object element = getCell(y, x); if (!(element instanceof Double)) { throw new Exception("Can't use non-fixed costs in " + "computing expected costs."); } costs[x] += classProbs[y] * ((Double) element).doubleValue(); } } return costs; }
/** * 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]. * * @return the matrix in Matlab single line format */ public String toMatlab() { StringBuffer result; int i; int n; result = new StringBuffer(); result.append("["); for (i = 0; i < m_size; i++) { if (i > 0) { result.append("; "); } for (n = 0; n < m_size; n++) { if (n > 0) { result.append(" "); } result.append(getCell(i, n)); } } result.append("]"); return result.toString(); }
/** * 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]. * * @return the matrix in Matlab single line format */ public String toMatlab() { StringBuffer result; int i; int n; result = new StringBuffer(); result.append("["); for (i = 0; i < m_size; i++) { if (i > 0) { result.append("; "); } for (n = 0; n < m_size; n++) { if (n > 0) { result.append(" "); } result.append(getCell(i, n)); } } result.append("]"); return result.toString(); }
/** * Creates a cost matrix that is a copy of another. * * @param toCopy the matrix to copy. */ public CostMatrix(CostMatrix toCopy) { this(toCopy.size()); for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { setCell(i, j, toCopy.getCell(i, j)); } } }
/** * Creates a cost matrix that is a copy of another. * * @param toCopy the matrix to copy. */ public CostMatrix(CostMatrix toCopy) { this(toCopy.size()); for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { setCell(i, j, toCopy.getCell(i, j)); } } }
/** * Gets the maximum cost for a particular class value. * * @param classVal the class value. * @return the maximum cost. * @exception Exception if cost matrix contains non-fixed costs */ public double getMaxCost(int classVal, Instance inst) throws Exception { if (!replaceStrings(inst.dataset())) { return getMaxCost(classVal); } double maxCost = Double.NEGATIVE_INFINITY; double cost; for (int i = 0; i < m_size; i++) { Object element = getCell(classVal, i); if (!(element instanceof Double)) { cost = ((InstanceExpression) element).evaluate(inst); } else { cost = ((Double) element).doubleValue(); } if (cost > maxCost) maxCost = cost; } return maxCost; }
/** * Gets the maximum cost for a particular class value. * * @param classVal the class value. * @return the maximum cost. * @exception Exception if cost matrix contains non-fixed costs */ public double getMaxCost(int classVal, Instance inst) throws Exception { if (!replaceStrings(inst.dataset())) { return getMaxCost(classVal); } double maxCost = Double.NEGATIVE_INFINITY; double cost; for (int i = 0; i < m_size; i++) { Object element = getCell(classVal, i); if (!(element instanceof Double)) { cost = ((InstanceExpression) element).evaluate(inst); } else { cost = ((Double) element).doubleValue(); } if (cost > maxCost) maxCost = cost; } return maxCost; }
/** * adds the given CostMatrix to a DOM structure. * * @param parent the parent of this object, e.g. the class this object is a * member of * @param o the Object to describe in XML * @param name the name of the object * @return the node that was created * @throws Exception if the DOM creation fails * @see weka.classifiers.CostMatrix */ public Element writeCostMatrix(Element parent, Object o, String name) throws Exception { weka.classifiers.CostMatrix matrix = (weka.classifiers.CostMatrix) o; Element node; // for debugging only if (DEBUG) { trace(new Throwable(), name); } m_CurrentNode = parent; node = addElement(parent, name, o.getClass().getName(), false); Object[][] m = new Object[matrix.size()][matrix.size()]; for (int i = 0; i < matrix.size(); i++) { for (int j = 0; j < matrix.size(); j++) { m[i][j] = matrix.getCell(i, j); } } invokeWriteToXML(node, m, VAL_CELLS); return node; }
/** * adds the given CostMatrix to a DOM structure. * * @param parent the parent of this object, e.g. the class this object is a * member of * @param o the Object to describe in XML * @param name the name of the object * @return the node that was created * @throws Exception if the DOM creation fails * @see weka.classifiers.CostMatrix */ public Element writeCostMatrix(Element parent, Object o, String name) throws Exception { weka.classifiers.CostMatrix matrix = (weka.classifiers.CostMatrix) o; Element node; // for debugging only if (DEBUG) { trace(new Throwable(), name); } m_CurrentNode = parent; node = addElement(parent, name, o.getClass().getName(), false); Object[][] m = new Object[matrix.size()][matrix.size()]; for (int i = 0; i < matrix.size(); i++) { for (int j = 0; j < matrix.size(); j++) { m[i][j] = matrix.getCell(i, j); } } invokeWriteToXML(node, m, VAL_CELLS); return node; }