/** * Constructs a new CostMatrixEditor. * */ public CostMatrixEditor() { m_matrix = new CostMatrix(2); m_propSupport = new PropertyChangeSupport(this); m_customEditor = new CustomEditor(); }
/** * Constructs a new CostMatrixEditor. * */ public CostMatrixEditor() { m_matrix = new CostMatrix(2); m_propSupport = new PropertyChangeSupport(this); m_customEditor = new CustomEditor(); }
/** * Prompts the user to open a matrix, and attemps to load it. * */ private void openMatrix() { int returnVal = m_fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File selectedFile = m_fileChooser.getSelectedFile(); Reader reader = null; try { reader = new BufferedReader(new FileReader(selectedFile)); m_matrix = new CostMatrix(reader); reader.close(); matrixChanged(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error reading file '" + selectedFile.getName() + "':\n" + ex.getMessage(), "Load failed", JOptionPane.ERROR_MESSAGE); System.out.println(ex.getMessage()); } } }
/** * Prompts the user to open a matrix, and attemps to load it. * */ private void openMatrix() { int returnVal = m_fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File selectedFile = m_fileChooser.getSelectedFile(); Reader reader = null; try { reader = new BufferedReader(new FileReader(selectedFile)); m_matrix = new CostMatrix(reader); reader.close(); matrixChanged(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error reading file '" + selectedFile.getName() + "':\n" + ex.getMessage(), "Load failed", JOptionPane.ERROR_MESSAGE); System.out.println(ex.getMessage()); } } }
/** * builds the Matrix (old) from the given DOM node. * * @param node the associated XML node * @return the instance created from the XML description * @throws Exception if instantiation fails * @see weka.classifiers.CostMatrix */ public Object readCostMatrixOld(Element node) throws Exception { weka.classifiers.CostMatrix matrix; weka.core.matrix.Matrix matrixNew; StringWriter writer; // for debugging only if (DEBUG) { trace(new Throwable(), node.getAttribute(ATT_NAME)); } m_CurrentNode = node; matrixNew = (weka.core.matrix.Matrix) readMatrix(node); writer = new StringWriter(); matrixNew.write(writer); matrix = new weka.classifiers.CostMatrix(new StringReader(writer.toString())); return matrix; }
/** * builds the Matrix (old) from the given DOM node. * * @param node the associated XML node * @return the instance created from the XML description * @throws Exception if instantiation fails * @see weka.classifiers.CostMatrix */ public Object readCostMatrixOld(Element node) throws Exception { weka.classifiers.CostMatrix matrix; weka.core.matrix.Matrix matrixNew; StringWriter writer; // for debugging only if (DEBUG) { trace(new Throwable(), node.getAttribute(ATT_NAME)); } m_CurrentNode = node; matrixNew = (weka.core.matrix.Matrix) readMatrix(node); writer = new StringWriter(); matrixNew.write(writer); matrix = new weka.classifiers.CostMatrix(new StringReader(writer.toString())); return matrix; }
/** * Responds to the user perfoming an action. * * @param e the action event that occured */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == m_defaultButton) { m_matrix.initialize(); matrixChanged(); } else if (e.getSource() == m_openButton) { openMatrix(); } else if (e.getSource() == m_saveButton) { saveMatrix(); } else if ((e.getSource() == m_classesField) || (e.getSource() == m_resizeButton)) { try { int newNumClasses = Integer.parseInt(m_classesField.getText()); if (newNumClasses > 0 && newNumClasses != m_matrix.size()) { setValue(new CostMatrix(newNumClasses)); } } catch (Exception ex) { } } }
/** * Responds to the user perfoming an action. * * @param e the action event that occured */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == m_defaultButton) { m_matrix.initialize(); matrixChanged(); } else if (e.getSource() == m_openButton) { openMatrix(); } else if (e.getSource() == m_saveButton) { saveMatrix(); } else if ((e.getSource() == m_classesField) || (e.getSource() == m_resizeButton)) { try { int newNumClasses = Integer.parseInt(m_classesField.getText()); if (newNumClasses > 0 && newNumClasses != m_matrix.size()) { setValue(new CostMatrix(newNumClasses)); } } catch (Exception ex) { } } }
throw new Exception("On-demand cost file doesn't exist: " + costFile); setCostMatrix(new CostMatrix(new BufferedReader( new FileReader(costFile)))); } else if (m_CostMatrix == null) { m_CostMatrix = new CostMatrix(data.numClasses()); m_CostMatrix.readOldFormat(new BufferedReader( new FileReader(m_CostFile)));
throw new Exception("On-demand cost file doesn't exist: " + costFile); setCostMatrix(new CostMatrix(new BufferedReader( new FileReader(costFile)))); } else if (m_CostMatrix == null) { m_CostMatrix = new CostMatrix(data.numClasses()); m_CostMatrix.readOldFormat(new BufferedReader( new FileReader(m_CostFile)));
matrix = new weka.classifiers.CostMatrix(m.length); for (int j = 0; j < matrix.size(); j++) { for (int k = 0; k < matrix.size(); k++) {
matrix = new weka.classifiers.CostMatrix(m.length); for (int j = 0; j < matrix.size(); j++) { for (int k = 0; k < matrix.size(); k++) {
/** Creates a default CostSensitiveClassifier */ public Classifier getClassifier() { CostSensitiveClassifier cl = new CostSensitiveClassifier(); // load costmatrix try { cl.setCostMatrix( new CostMatrix( new InputStreamReader(ClassLoader.getSystemResourceAsStream( "weka/classifiers/data/ClassifierTest.cost")))); } catch (Exception e) { e.printStackTrace(); } return cl; }
/** Creates a default CostSensitiveClassifier */ public Classifier getClassifier() { CostSensitiveClassifier cl = new CostSensitiveClassifier(); // load costmatrix try { cl.setCostMatrix( new CostMatrix( new InputStreamReader(ClassLoader.getSystemResourceAsStream( "weka/classifiers/data/ClassifierTest.cost")))); } catch (Exception e) { e.printStackTrace(); } return cl; }
/** * Get a 2 class cost matrix with the supplied off diagonal costs set * * @param cost1 first cost value * @param cost2 second cost value * @return */ protected CostMatrix get2ClassCostMatrixNoExpressions(double cost1, double cost2) { CostMatrix matrix = new CostMatrix(2); matrix.setCell(0, 1, cost1); matrix.setCell(1, 0, cost2); return matrix; }
/** * Get a 2 class cost matrix with the supplied off diagonal costs set * * @param cost1 first cost value * @param cost2 second cost value * @return */ protected CostMatrix get2ClassCostMatrixNoExpressions(double cost1, double cost2) { CostMatrix matrix = new CostMatrix(2); matrix.setCell(0, 1, cost1); matrix.setCell(1, 0, cost2); return matrix; }
/** * Tests creating a 3x3 cost matrix and applying it to 2 class data. An * execption is expected in this case * * @throws Exception if a problem occurs */ public void testIncorrectSize() throws Exception { CostMatrix matrix = new CostMatrix(3); Instances data = getData(); try { matrix.applyCostMatrix(data, null); fail("Was expecting an exception as the cost matrix represents more classes than " + "are present in the data"); } catch (Exception ex) { // expected } }
/** * Tests creating a 3x3 cost matrix and applying it to 2 class data. An * execption is expected in this case * * @throws Exception if a problem occurs */ public void testIncorrectSize() throws Exception { CostMatrix matrix = new CostMatrix(3); Instances data = getData(); try { matrix.applyCostMatrix(data, null); fail("Was expecting an exception as the cost matrix represents more classes than " + "are present in the data"); } catch (Exception ex) { // expected } }
costSens.setClassifier(curClassifier); CostMatrix cm = new CostMatrix(2); // [0.0 95.0; 5.0 0.0] cm.initialize();
if (numClasses != ((CostMatrix) m_CostMatrixEditor.getValue()) .numColumns()) { m_CostMatrixEditor.setValue(new CostMatrix(numClasses));