List of usage examples for weka.classifiers Evaluation predictions
public ArrayList<Prediction> predictions()
From source file:TextClassifierUI.java
private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed // TODO add your handling code here: try {//from w ww . java2 s . c o m DocClassifier dr = new DocClassifier(trainFiles, testFiles); Classifier cl; if (naiveBayes.isSelected()) { cl = new NaiveBayes(); } else { cl = new IBk(Integer.parseInt(kNearest.getText())); } Evaluation ev; if (useCV.isSelected()) { ev = dr.cvClassify(cl, Integer.parseInt(kFold.getText())); result.setText(dr.performanceEval(ev)); } else { ev = dr.classify(cl); result.setText(dr.performanceEval(ev)); result.append("\nDOCUMENT\t=>\tPREDICT\n"); for (String p : dr.getDocPredList()) { result.append(p + "\n"); } } ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); setVMC(ev.predictions(), vmc, true); showVMC(vmc); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "K Nearest and K-Fold must be positive numbers.", "Number Format Error", JOptionPane.ERROR_MESSAGE); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to classify : " + e.getLocalizedMessage(), "Unexpected Error", JOptionPane.ERROR_MESSAGE); } }
From source file:CopiaSeg3.java
public static void main(String[] args) throws Exception { BufferedReader datafile = readDataFile("breast-cancer-wisconsin.arff"); Instances data = new Instances(datafile); data.setClassIndex(data.numAttributes() - 1); // Elije el nmero de particiones para la valicacin (4 = 75% Train, 25% Test) Instances[] split = split(data, 4);/*ww w. j a v a 2 s .com*/ // Separa los conjuntos en los arrays trainning y testing Instances trainingSplits = split[0]; Instances testingSplits = split[1]; // Elegir un conjunto de clasificadores Classifier[] models = { new MultilayerPerceptron() //, new J48 //, ... }; FastVector fvWekaAttributes = new FastVector(9); // Ejecutar cada clasificador for (int j = 0; j < models.length; j++) { // Collect every group of predictions for current model in a FastVector FastVector predictions = new FastVector(); // For each training-testing split pair, train and test the classifier Evaluation validation = simpleClassify(models[j], trainingSplits, testingSplits); predictions.appendElements(validation.predictions()); // Uncomment to see the summary for each training-testing pair. System.out.println(models[j].toString()); // Calculate overall accuracy of current classifier on all splits double accuracy = calculateAccuracy(predictions); // // Print current classifier's name and accuracy in a complicated, but nice-looking way. System.out.println(models[j].getClass().getSimpleName() + " Accuracy: " + String.format("%.2f%%", accuracy) + "\n====================="); // // // Step 4: use the classifier // // For real world applications, the actual use of the classifier is the ultimate goal. Heres the simplest way to achieve that. Lets say weve built an instance (named iUse) as explained in step 2: // // Specify that the instance belong to the training set // // in order to inherit from the set description Instance iUse = new DenseInstance(9); iUse.setValue((Attribute) predictions.elementAt(0), 4); iUse.setValue((Attribute) predictions.elementAt(1), 8); iUse.setValue((Attribute) predictions.elementAt(2), 8); iUse.setValue((Attribute) predictions.elementAt(3), 5); iUse.setValue((Attribute) predictions.elementAt(4), 4); iUse.setValue((Attribute) predictions.elementAt(5), 5); iUse.setValue((Attribute) predictions.elementAt(6), 10); iUse.setValue((Attribute) predictions.elementAt(7), 4); iUse.setValue((Attribute) predictions.elementAt(8), 1); iUse.setDataset(trainingSplits); // // // Get the likelihood of each classes // fDistribution[0] is the probability of being positive? // fDistribution[1] is the probability of being negative? double[] fDistribution = models[j].distributionForInstance(iUse); System.out.println("Probabilidad positivo: " + fDistribution[0]); System.out.println("Probabilidad negativo: " + fDistribution[1]); } }
From source file:adams.data.conversion.WekaEvaluationToCostCurve.java
License:Open Source License
/** * Performs the actual conversion./*from w ww . j a v a 2 s .com*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Evaluation eval; CostCurve curve; Instances cost; eval = (Evaluation) m_Input; m_ClassLabelIndex.setMax(eval.getHeader().classAttribute().numValues()); curve = new CostCurve(); cost = curve.getCurve(eval.predictions(), m_ClassLabelIndex.getIntIndex()); return cost; }
From source file:adams.data.conversion.WekaEvaluationToMarginCurve.java
License:Open Source License
/** * Performs the actual conversion./*from w ww . j a v a 2s. c o m*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Evaluation eval; MarginCurve curve; Instances cost; eval = (Evaluation) m_Input; curve = new MarginCurve(); cost = curve.getCurve(eval.predictions()); return cost; }
From source file:adams.data.conversion.WekaEvaluationToThresholdCurve.java
License:Open Source License
/** * Performs the actual conversion./*from www. j av a2 s . co m*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Evaluation eval; ThresholdCurve curve; Instances cost; eval = (Evaluation) m_Input; m_ClassLabelIndex.setMax(eval.getHeader().classAttribute().numValues()); curve = new ThresholdCurve(); cost = curve.getCurve(eval.predictions(), m_ClassLabelIndex.getIntIndex()); return cost; }
From source file:adams.flow.sink.WekaClassifierErrors.java
License:Open Source License
/** * ClassifierErrorss the token (the panel and dialog have already been created at * this stage)./*from w ww.j av a 2 s. c om*/ * * @param token the token to display */ @Override protected void display(Token token) { DataGenerator generator; Evaluation eval; try { if (token.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) token.getPayload(); if (eval.predictions() == null) { getLogger().severe("No predictions available from Evaluation object!"); return; } generator = new DataGenerator(eval, m_ErrorScaler); PlotData2D plotdata = generator.getPlotData(); plotdata.setPlotName(generator.getPlotInstances().relationName()); m_VisualizePanel.addPlot(plotdata); m_VisualizePanel.setColourIndex(plotdata.getPlotInstances().classIndex()); if ((m_VisualizePanel.getXIndex() == 0) && (m_VisualizePanel.getYIndex() == 1)) { try { m_VisualizePanel.setXIndex(m_VisualizePanel.getInstances().classIndex()); // class m_VisualizePanel.setYIndex(m_VisualizePanel.getInstances().classIndex() - 1); // predicted class } catch (Exception e) { // ignored } } } catch (Exception e) { handleException("Failed to display token: " + token, e); } }
From source file:adams.flow.sink.WekaCostBenefitAnalysis.java
License:Open Source License
/** * Plots the token (the panel and dialog have already been created at * this stage).// w w w. j a v a2 s.co m * * @param token the token to display */ @Override protected void display(Token token) { Evaluation eval; Attribute classAtt; Attribute classAttToUse; int classValue; ThresholdCurve tc; Instances result; ArrayList<String> newNames; CostBenefitAnalysis cbAnalysis; PlotData2D tempd; boolean[] cp; int n; try { if (token.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) token.getPayload(); if (eval.predictions() == null) { getLogger().severe("No predictions available from Evaluation object!"); return; } classAtt = eval.getHeader().classAttribute(); m_ClassIndex.setData(classAtt); classValue = m_ClassIndex.getIntIndex(); tc = new ThresholdCurve(); result = tc.getCurve(eval.predictions(), classValue); // Create a dummy class attribute with the chosen // class value as index 0 (if necessary). classAttToUse = eval.getHeader().classAttribute(); if (classValue != 0) { newNames = new ArrayList<>(); newNames.add(classAtt.value(classValue)); for (int k = 0; k < classAtt.numValues(); k++) { if (k != classValue) newNames.add(classAtt.value(k)); } classAttToUse = new Attribute(classAtt.name(), newNames); } // assemble plot data tempd = new PlotData2D(result); tempd.setPlotName(result.relationName()); tempd.m_alwaysDisplayPointsOfThisSize = 10; // specify which points are connected cp = new boolean[result.numInstances()]; for (n = 1; n < cp.length; n++) cp[n] = true; tempd.setConnectPoints(cp); // add plot m_CostBenefitPanel.setCurveData(tempd, classAttToUse); } catch (Exception e) { handleException("Failed to display token: " + token, e); } }
From source file:adams.flow.sink.WekaCostBenefitAnalysis.java
License:Open Source License
/** * Creates a new panel for the token./*from w w w . j a va2 s.co m*/ * * @param token the token to display in a new panel, can be null * @return the generated panel */ public AbstractDisplayPanel createDisplayPanel(Token token) { AbstractDisplayPanel result; String name; if (token != null) name = "Cost curve (" + getEvaluation(token).getHeader().relationName() + ")"; else name = "Cost curve"; result = new AbstractComponentDisplayPanel(name) { private static final long serialVersionUID = -3513994354297811163L; protected CostBenefitAnalysis m_VisualizePanel; @Override protected void initGUI() { super.initGUI(); setLayout(new BorderLayout()); m_VisualizePanel = new CostBenefitAnalysis(); add(m_VisualizePanel, BorderLayout.CENTER); } @Override public void display(Token token) { try { Evaluation eval = getEvaluation(token); Attribute classAtt = eval.getHeader().classAttribute(); m_ClassIndex.setData(classAtt); int classValue = m_ClassIndex.getIntIndex(); ThresholdCurve tc = new ThresholdCurve(); Instances result = tc.getCurve(eval.predictions(), classValue); // Create a dummy class attribute with the chosen // class value as index 0 (if necessary). Attribute classAttToUse = eval.getHeader().classAttribute(); if (classValue != 0) { ArrayList<String> newNames = new ArrayList<>(); newNames.add(classAtt.value(classValue)); for (int k = 0; k < classAtt.numValues(); k++) { if (k != classValue) newNames.add(classAtt.value(k)); } classAttToUse = new Attribute(classAtt.name(), newNames); } // assemble plot data PlotData2D tempd = new PlotData2D(result); tempd.setPlotName(result.relationName()); tempd.m_alwaysDisplayPointsOfThisSize = 10; // specify which points are connected boolean[] cp = new boolean[result.numInstances()]; for (int n = 1; n < cp.length; n++) cp[n] = true; tempd.setConnectPoints(cp); // add plot m_VisualizePanel.setCurveData(tempd, classAttToUse); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to display token: " + token, e); } } @Override public JComponent supplyComponent() { return m_VisualizePanel; } @Override public void clearPanel() { } public void cleanUp() { } }; if (token != null) result.display(token); return result; }
From source file:adams.flow.sink.WekaCostCurve.java
License:Open Source License
/** * Plots the token (the panel and dialog have already been created at * this stage).// w w w.j a v a 2s . com * * @param token the token to display */ @Override protected void display(Token token) { weka.classifiers.evaluation.CostCurve curve; Evaluation eval; PlotData2D plot; boolean[] connectPoints; int cp; Instances data; int[] indices; try { if (token.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) token.getPayload(); if (eval.predictions() == null) { getLogger().severe("No predictions available from Evaluation object!"); return; } m_ClassLabelRange.setData(eval.getHeader().classAttribute()); indices = m_ClassLabelRange.getIntIndices(); for (int index : indices) { curve = new weka.classifiers.evaluation.CostCurve(); data = curve.getCurve(eval.predictions(), index); plot = new PlotData2D(data); plot.setPlotName(eval.getHeader().classAttribute().value(index)); plot.m_displayAllPoints = true; connectPoints = new boolean[data.numInstances()]; for (cp = 1; cp < connectPoints.length; cp++) connectPoints[cp] = true; plot.setConnectPoints(connectPoints); m_VisualizePanel.addPlot(plot); } } catch (Exception e) { handleException("Failed to display token: " + token, e); } }
From source file:adams.flow.sink.WekaCostCurve.java
License:Open Source License
/** * Creates a new panel for the token.// ww w. j a v a 2s. c om * * @param token the token to display in a new panel, can be null * @return the generated panel */ public AbstractDisplayPanel createDisplayPanel(Token token) { AbstractDisplayPanel result; String name; if (token != null) name = "Cost curve (" + getEvaluation(token).getHeader().relationName() + ")"; else name = "Cost curve"; result = new AbstractComponentDisplayPanel(name) { private static final long serialVersionUID = -3513994354297811163L; protected VisualizePanel m_VisualizePanel; @Override protected void initGUI() { super.initGUI(); setLayout(new BorderLayout()); m_VisualizePanel = new VisualizePanel(); add(m_VisualizePanel, BorderLayout.CENTER); } @Override public void display(Token token) { try { Evaluation eval = getEvaluation(token); m_ClassLabelRange.setMax(eval.getHeader().classAttribute().numValues()); int[] indices = m_ClassLabelRange.getIntIndices(); for (int index : indices) { weka.classifiers.evaluation.CostCurve curve = new weka.classifiers.evaluation.CostCurve(); Instances data = curve.getCurve(eval.predictions(), index); PlotData2D plot = new PlotData2D(data); plot.setPlotName(eval.getHeader().classAttribute().value(index)); plot.m_displayAllPoints = true; boolean[] connectPoints = new boolean[data.numInstances()]; for (int cp = 1; cp < connectPoints.length; cp++) connectPoints[cp] = true; plot.setConnectPoints(connectPoints); m_VisualizePanel.addPlot(plot); } } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to display token: " + token, e); } } @Override public JComponent supplyComponent() { return m_VisualizePanel; } @Override public void clearPanel() { m_VisualizePanel.removeAllPlots(); } public void cleanUp() { m_VisualizePanel.removeAllPlots(); } }; if (token != null) result.display(token); return result; }