List of usage examples for weka.core Instances setClassIndex
public void setClassIndex(int classIndex)
From source file:meka.gui.explorer.classify.PredictionsOnTestset.java
License:Open Source License
/** * Returns the action lister to use in the menu. * * @param history the current history//from w w w. j a v a 2 s. c om * @param index the selected history item * @return the listener */ @Override public ActionListener getActionListener(final ResultHistoryList history, final int index) { final MultiLabelClassifier classifier = (MultiLabelClassifier) getClassifier(history, index); final Instances header = getHeader(history, index); return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Runnable run = new Runnable() { @Override public void run() { ClassifyTab owner = (ClassifyTab) getOwner(); Instances test; owner.startBusy("Predictions on test..."); try { MLUtils.prepareData(owner.getTestData()); test = new Instances(owner.getTestData()); test.setClassIndex(owner.getTestData().classIndex()); String msg = header.equalHeadersMsg(test); if (msg != null) throw new IllegalArgumentException( "Model's training set and current test set are not compatible:\n" + msg); // collect predictions Instances predicted = new Instances(test, 0); for (int i = 0; i < test.numInstances(); i++) { double pred[] = classifier.distributionForInstance(test.instance(i)); // Cut off any [no-longer-needed] probabalistic information from MT classifiers. if (classifier instanceof MultiTargetClassifier) pred = Arrays.copyOf(pred, test.classIndex()); Instance predInst = (Instance) test.instance(i).copy(); for (int j = 0; j < pred.length; j++) predInst.setValue(j, pred[j]); predicted.add(predInst); if ((i + 1) % 100 == 0) owner.showStatus( "Predictions on test (" + (i + 1) + "/" + test.numInstances() + ")..."); } owner.finishBusy(); // display predictions DataViewerDialog dialog = new DataViewerDialog(GUIHelper.getParentFrame(owner), ModalityType.MODELESS); dialog.setDefaultCloseOperation(DataViewerDialog.DISPOSE_ON_CLOSE); dialog.setInstances(predicted); dialog.setSize(800, 600); dialog.setLocationRelativeTo(owner); dialog.setVisible(true); } catch (Exception e) { owner.handleException("Predictions failed on test set:", e); owner.finishBusy("Predictions failed: " + e); JOptionPane.showMessageDialog(owner, "Predictions failed:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; ((ClassifyTab) getOwner()).start(run); } }; }
From source file:meka.gui.explorer.classify.ReevaluateModelOnTestset.java
License:Open Source License
/** * Returns the action lister to use in the menu. * * @param history the current history//from w w w .j a v a2 s. c o m * @param index the selected history item * @return the listener */ @Override public ActionListener getActionListener(final ResultHistoryList history, final int index) { final MultiLabelClassifier classifier = (MultiLabelClassifier) getClassifier(history, index); final Instances header = getHeader(history, index); return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Runnable run = new Runnable() { @Override public void run() { ClassifyTab owner = (ClassifyTab) getOwner(); Result result; Instances test; owner.startBusy("Reevaluate on test..."); try { MLUtils.prepareData(owner.getTestData()); test = new Instances(owner.getTestData()); test.setClassIndex(owner.getTestData().classIndex()); String msg = header.equalHeadersMsg(test); if (msg != null) throw new IllegalArgumentException( "Model's training set and current test set are not compatible:\n" + msg); owner.log(OptionUtils.toCommandLine(classifier)); owner.log("Testset: " + test.relationName()); owner.log("Class-index: " + test.classIndex()); result = Evaluation.evaluateModel(classifier, test, "0.0", owner.getVOP()); // TODO what threshold to use? owner.addResultToHistory(result, new Object[] { classifier, new Instances(test, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); owner.finishBusy(); } catch (Exception e) { owner.handleException("Reevaluation failed on test set:", e); owner.finishBusy("Reevaluation failed: " + e); JOptionPane.showMessageDialog(owner, "Reevaluation failed:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; ((ClassifyTab) getOwner()).start(run); } }; }
From source file:meka.gui.explorer.ClassifyTab.java
License:Open Source License
/** * Starts the classification.//from www .j a v a 2 s. com */ protected void startClassification() { String type; Runnable run; final Instances data; if (m_ComboBoxExperiment.getSelectedIndex() == -1) return; data = new Instances(getData()); if (m_Randomize) data.randomize(new Random(m_Seed)); type = m_ComboBoxExperiment.getSelectedItem().toString(); run = null; switch (type) { case TYPE_CROSSVALIDATION: run = new Runnable() { @Override public void run() { MultiLabelClassifier classifier; Result result; startBusy("Cross-validating..."); try { classifier = (MultiLabelClassifier) m_GenericObjectEditor.getValue(); log(OptionUtils.toCommandLine(classifier)); log("Dataset: " + data.relationName()); log("Class-index: " + data.classIndex()); result = Evaluation.cvModel(classifier, data, m_Folds, m_TOP, m_VOP); addResultToHistory(result, new Object[] { classifier, new Instances(data, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); finishBusy(); } catch (Exception e) { handleException("Evaluation failed:", e); finishBusy("Evaluation failed: " + e); JOptionPane.showMessageDialog(ClassifyTab.this, "Evaluation failed (CV):\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; break; case TYPE_TRAINTESTSPLIT: run = new Runnable() { @Override public void run() { MultiLabelClassifier classifier; Result result; int trainSize; Instances train; Instances test; startBusy("Train/test split..."); try { trainSize = (int) (data.numInstances() * m_SplitPercentage / 100.0); train = new Instances(data, 0, trainSize); test = new Instances(data, trainSize, data.numInstances() - trainSize); classifier = (MultiLabelClassifier) m_GenericObjectEditor.getValue(); log(OptionUtils.toCommandLine(classifier)); log("Dataset: " + train.relationName()); log("Class-index: " + train.classIndex()); result = Evaluation.evaluateModel(classifier, train, test, m_TOP, m_VOP); addResultToHistory(result, new Object[] { classifier, new Instances(train, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); finishBusy(); } catch (Exception e) { handleException("Evaluation failed (train/test split):", e); finishBusy("Evaluation failed: " + e); JOptionPane.showMessageDialog(ClassifyTab.this, "Evaluation failed:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; break; case TYPE_SUPPLIEDTESTSET: run = new Runnable() { @Override public void run() { MultiLabelClassifier classifier; Result result; int trainSize; Instances train; Instances test; startBusy("Supplied test..."); try { train = new Instances(data); MLUtils.prepareData(m_TestInstances); test = new Instances(m_TestInstances); test.setClassIndex(data.classIndex()); String msg = train.equalHeadersMsg(test); if (msg != null) throw new IllegalArgumentException("Train and test set are not compatible:\n" + msg); classifier = (MultiLabelClassifier) m_GenericObjectEditor.getValue(); log(OptionUtils.toCommandLine(classifier)); log("Dataset: " + train.relationName()); log("Class-index: " + train.classIndex()); result = Evaluation.evaluateModel(classifier, train, test, m_TOP, m_VOP); addResultToHistory(result, new Object[] { classifier, new Instances(train, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); finishBusy(); } catch (Exception e) { handleException("Evaluation failed (train/test split):", e); finishBusy("Evaluation failed: " + e); JOptionPane.showMessageDialog(ClassifyTab.this, "Evaluation failed:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; break; case TYPE_BINCREMENTAL: run = new Runnable() { @Override public void run() { MultiLabelClassifier classifier; Result result; startBusy("Incremental..."); try { classifier = (MultiLabelClassifier) m_GenericObjectEditor.getValue(); log(OptionUtils.toCommandLine(classifier)); log("Dataset: " + data.relationName()); log("Class-index: " + data.classIndex()); result = IncrementalEvaluation.evaluateModelBatchWindow(classifier, data, m_Samples, 1., m_TOP, m_VOP); addResultToHistory(result, new Object[] { classifier, new Instances(data, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); finishBusy(); } catch (Exception e) { handleException("Evaluation failed (incremental splits):", e); finishBusy("Evaluation failed: " + e); JOptionPane.showMessageDialog(ClassifyTab.this, "Evaluation failed:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; break; case TYPE_PREQUENTIAL: run = new Runnable() { @Override public void run() { MultiLabelClassifier classifier; Result result; startBusy("Incremental..."); try { classifier = (MultiLabelClassifier) m_GenericObjectEditor.getValue(); log(OptionUtils.toCommandLine(classifier)); log("Dataset: " + data.relationName()); log("Class-index: " + data.classIndex()); result = IncrementalEvaluation.evaluateModelPrequentialBasic(classifier, data, (data.numInstances() / (m_Samples + 1)), 1., m_TOP, m_VOP); addResultToHistory(result, new Object[] { classifier, new Instances(data, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); finishBusy(); } catch (Exception e) { handleException("Evaluation failed (incremental splits):", e); finishBusy("Evaluation failed: " + e); JOptionPane.showMessageDialog(ClassifyTab.this, "Evaluation failed:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); } } }; break; default: throw new IllegalStateException("Unhandled evaluation type: " + type); } start(run); }
From source file:meka.gui.explorer.Explorer.java
License:Open Source License
/** * edits the current instances object in the viewer *///from w w w .jav a 2s .c o m public void edit() { ViewerDialog dialog; int result; Instances copy; Instances newInstances; copy = new Instances(m_Data); dialog = new ViewerDialog(null); dialog.setSize(800, 600); dialog.setLocationRelativeTo(this); result = dialog.showDialog(copy); if (result == ViewerDialog.APPROVE_OPTION) { try { addUndoPoint(); } catch (Exception e) { e.printStackTrace(); } // if class was not set before, reset it again after use of filter newInstances = dialog.getInstances(); if (m_Data.classIndex() < 0) newInstances.setClassIndex(-1); notifyTabsDataChanged(null, newInstances); } }
From source file:meka.gui.guichooser.PrecisionRecallCurve.java
License:Open Source License
/** * Called by the menu items action listener. *//* w ww. j a v a2s .c om*/ @Override protected void launch() { m_FileChooser = GUIHelper.newConverterFileChooser(); // choose file int retVal = m_FileChooser.showOpenDialog(null); if (retVal != JFileChooser.APPROVE_OPTION) return; File file = m_FileChooser.getSelectedFile(); // create plot Instances data; try { data = m_FileChooser.getLoader().getDataSet(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error loading file '" + file + "':\n" + e, "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } data.setClassIndex(data.numAttributes() - 1); ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); vmc.setROCString("(Area under PRC = " + Utils.doubleToString(ThresholdCurve.getPRCArea(data), 4) + ")"); vmc.setName(data.relationName()); PlotData2D tempd = new PlotData2D(data); tempd.setPlotName(data.relationName()); tempd.m_displayAllPoints = true; // specify which points are connected boolean[] cp = new boolean[data.numInstances()]; for (int n = 1; n < cp.length; n++) cp[n] = true; try { tempd.setConnectPoints(cp); vmc.addPlot(tempd); if (data.attribute(ThresholdCurve.RECALL_NAME) != null) vmc.setXIndex(data.attribute(ThresholdCurve.RECALL_NAME).index()); if (data.attribute(ThresholdCurve.PRECISION_NAME) != null) vmc.setYIndex(data.attribute(ThresholdCurve.PRECISION_NAME).index()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error adding plot:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } MekaFrame frame = new MekaFrame(); frame.setTitle(getName()); frame.setDefaultCloseOperation(MekaFrame.DISPOSE_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(vmc); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:meka.gui.guichooser.ROC.java
License:Open Source License
/** * Called by the menu items action listener. *///from ww w . jav a 2 s . c o m @Override protected void launch() { m_FileChooser = GUIHelper.newConverterFileChooser(); // choose file int retVal = m_FileChooser.showOpenDialog(null); if (retVal != JFileChooser.APPROVE_OPTION) return; File file = m_FileChooser.getSelectedFile(); // create plot Instances data; try { data = m_FileChooser.getLoader().getDataSet(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error loading file '" + file + "':\n" + e, "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } data.setClassIndex(data.numAttributes() - 1); ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); vmc.setROCString("(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(data), 4) + ")"); vmc.setName(data.relationName()); PlotData2D tempd = new PlotData2D(data); tempd.setPlotName(data.relationName()); tempd.m_displayAllPoints = true; // specify which points are connected boolean[] cp = new boolean[data.numInstances()]; for (int n = 1; n < cp.length; n++) cp[n] = true; try { tempd.setConnectPoints(cp); vmc.addPlot(tempd); if (data.attribute(ThresholdCurve.FP_RATE_NAME) != null) vmc.setXIndex(data.attribute(ThresholdCurve.FP_RATE_NAME).index()); if (data.attribute(ThresholdCurve.TP_RATE_NAME) != null) vmc.setYIndex(data.attribute(ThresholdCurve.TP_RATE_NAME).index()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error adding plot:\n" + e, "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } MekaFrame frame = new MekaFrame(); frame.setTitle(getName()); frame.setDefaultCloseOperation(MekaFrame.DISPOSE_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(vmc); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:MetaBlocking.EnhancedMetaBlocking.FastImplementations.RedefinedCardinalityNodePruning.java
License:Open Source License
@Override protected void pruneEdges(List<AbstractBlock> newBlocks, AbstractDuplicatePropagation adp) { nearestEntities = new Set[noOfEntities]; topKEdges = new PriorityQueue<Comparison>((int) (2 * threshold), new ComparisonWeightComparator()); this.adp = adp; getStatistics(newBlocks);/* w w w. j av a 2s . com*/ getAttributes(); int matchingInstances = 3000; HashSet<Comparison> trainingSet = new HashSet<Comparison>(4 * matchingInstances); Instances trainingInstances = new Instances("trainingSet", attributes, 2 * matchingInstances); trainingInstances.setClassIndex(noOfAttributes - 1); newBlocks.clear(); if (weightingScheme.equals(WeightingScheme.ARCS)) { for (int i = 0; i < noOfEntities; i++) { processArcsEntity(i); verifyValidEntities(i, trainingInstances); } } else { for (int i = 0; i < noOfEntities; i++) { processEntity(i); verifyValidEntities(i, trainingInstances); } } try { ArffSaver saver = new ArffSaver(); saver.setInstances(trainingInstances); saver.setFile(new File("/tmp/test.arff")); saver.setDestination(new File("/tmp/test.arff")); // **not** necessary in 3.5.4 and later saver.writeBatch(); } catch (Exception e) { System.err.println("ERRO SALVAMENTO ARQUIVO"); e.getStackTrace(); } retainValidComparisons(newBlocks); }
From source file:milk.classifiers.MIEvaluation.java
License:Open Source License
/** * Evaluates a classifier with the options given in an array of * strings. <p>/*w ww .j a v a 2s .c o m*/ * * Valid options are: <p> * * -t filename <br> * Name of the file with the training data. (required) <p> * * -T filename <br> * Name of the file with the test data. If missing a cross-validation * is performed. <p> * * -c index <br> * Index of the class attribute (1, 2, ...; default: last). <p> * * -I index <br> * Index of the ID attribute (0, 1, 2, ...; default: first). <p> * * -x number <br> * The number of folds for the cross-validation (default: 10). <p> * * -s seed <br> * Random number seed for the cross-validation (default: 1). <p> * * -m filename <br> * The name of a file containing a cost matrix. <p> * * -l filename <br> * Loads classifier from the given file. <p> * * -g <br> * Only for classifiers that implement "Graphable." Outputs * the graph representation of the classifier (and nothing * else). <p> * * -L <br> * Whether use "Leave-One-Out" cross-validation. <p> * * -d filename <br> * Saves classifier built from the training data into the given file. <p> * * -v <br> * Outputs no statistics for the training data. <p> * * -o <br> * Outputs statistics only, not the classifier. <p> * * @param classifier machine learning classifier * @param options the array of string containing the options * @exception Exception if model could not be evaluated successfully * @return a string describing the results */ public static String evaluateModel(MIClassifier classifier, String[] options) throws Exception { Exemplars train = null, tempTrain, test = null, template = null; int seed = 1, folds = 10, classIndex = -1, idIndex = -1; String trainFileName, testFileName, sourceClass, classIndexString, idIndexString, seedString, foldsString, objectInputFileName, objectOutputFileName, attributeRangeString; boolean IRstatistics = false, noOutput = false, leaveOneOut = false, printClassifications = false, trainStatistics = true, printMargins = false, printComplexityStatistics = false, classStatistics = true, printSource = false, printGraph = false; StringBuffer text = new StringBuffer(); BufferedReader trainReader = null, testReader = null; ObjectInputStream objectInputStream = null; Random random = null; CostMatrix costMatrix = null; StringBuffer schemeOptionsText = null; Range attributesToOutput = null; long trainTimeStart = 0, trainTimeElapsed = 0, testTimeStart = 0, testTimeElapsed = 0; Instances data = null; try { // Get basic options (options the same for all schemes) classIndexString = Utils.getOption('c', options); if (classIndexString.length() != 0) classIndex = Integer.parseInt(classIndexString); idIndexString = Utils.getOption('I', options); if (idIndexString.length() != 0) idIndex = Integer.parseInt(idIndexString); trainFileName = Utils.getOption('t', options); objectInputFileName = Utils.getOption('l', options); objectOutputFileName = Utils.getOption('d', options); testFileName = Utils.getOption('T', options); if (trainFileName.length() == 0) { if (objectInputFileName.length() == 0) { throw new Exception("No training file and no object " + "input file given."); } if (testFileName.length() == 0) { throw new Exception("No training file and no test " + "file given."); } } else if ((objectInputFileName.length() != 0) && ((!(classifier instanceof MIUpdateableClassifier)) || (testFileName.length() == 0))) { throw new Exception("Classifier not incremental, or no " + "test file provided: can't " + "use both train and model file."); } try { if (trainFileName.length() != 0) { trainReader = new BufferedReader(new FileReader(trainFileName)); } if (testFileName.length() != 0) testReader = new BufferedReader(new FileReader(testFileName)); if (objectInputFileName.length() != 0) { InputStream is = new FileInputStream(objectInputFileName); if (objectInputFileName.endsWith(".gz")) { is = new GZIPInputStream(is); } objectInputStream = new ObjectInputStream(is); } } catch (Exception e) { throw new Exception("Can't open file " + e.getMessage() + '.'); } if (testFileName.length() != 0) { Instances insts = new Instances(testReader); if (classIndex != -1) insts.setClassIndex(classIndex - 1); else insts.setClassIndex(insts.numAttributes() - 1); if (classIndex > insts.numAttributes()) throw new Exception("Index of class attribute too large."); if (idIndex != -1) test = new Exemplars(insts, idIndex); else test = new Exemplars(insts, 0); template = test; testReader.close(); } if (trainFileName.length() != 0) { data = new Instances(trainReader); if (classIndex != -1) data.setClassIndex(classIndex - 1); else data.setClassIndex(data.numAttributes() - 1); if (classIndex > data.numAttributes()) throw new Exception("Index of class attribute too large."); Instances tmp = new Instances(data); if (idIndex != -1) train = new Exemplars(tmp, idIndex); else train = new Exemplars(tmp, 0); template = train; trainReader.close(); } if (template == null) throw new Exception("No actual dataset provided to use as template"); seedString = Utils.getOption('s', options); if (seedString.length() != 0) { seed = Integer.parseInt(seedString); } foldsString = Utils.getOption('x', options); if (foldsString.length() != 0) { folds = Integer.parseInt(foldsString); } costMatrix = handleCostOption(Utils.getOption('m', options), template.numClasses()); printGraph = Utils.getFlag('g', options); sourceClass = Utils.getOption('z', options); printMargins = Utils.getFlag('r', options); printSource = (sourceClass.length() != 0); classStatistics = Utils.getFlag('i', options); leaveOneOut = Utils.getFlag('L', options); if (leaveOneOut) // Leave-one-out folds = template.numExemplars(); // If a model file is given, we can't process // scheme-specific options if (objectInputFileName.length() != 0) { Utils.checkForRemainingOptions(options); } else { // Set options for classifier if (classifier instanceof OptionHandler) { for (int i = 0; i < options.length; i++) { if (options[i].length() != 0) { if (schemeOptionsText == null) { schemeOptionsText = new StringBuffer(); } if (options[i].indexOf(' ') != -1) { schemeOptionsText.append('"' + options[i] + "\" "); } else { schemeOptionsText.append(options[i] + " "); } } } ((OptionHandler) classifier).setOptions(options); } } Utils.checkForRemainingOptions(options); } catch (Exception e) { e.printStackTrace(); throw new Exception("\nWeka exception: " + e.getMessage() + makeOptionString(classifier)); } // Setup up evaluation objects MIEvaluation trainingEvaluation = new MIEvaluation(new Exemplars(template), costMatrix); MIEvaluation testingEvaluation = new MIEvaluation(new Exemplars(template), costMatrix); if (objectInputFileName.length() != 0) { // Load classifier from file classifier = (MIClassifier) objectInputStream.readObject(); objectInputStream.close(); } // Build the classifier if no object file provided if ((classifier instanceof MIUpdateableClassifier) && (testFileName.length() != 0) && (costMatrix == null) && (trainFileName.length() != 0)) { // Build classifier incrementally int x = 0; Exemplars traineg = new Exemplars(train.exemplar(x++).getInstances(), train.idIndex()); trainingEvaluation.setPriors(traineg); testingEvaluation.setPriors(traineg); trainTimeStart = System.currentTimeMillis(); if (objectInputFileName.length() == 0) { classifier.buildClassifier(traineg); } while (x < train.numExemplars()) { trainingEvaluation.updatePriors(train.exemplar(x)); testingEvaluation.updatePriors(train.exemplar(x)); ((MIUpdateableClassifier) classifier).updateClassifier(train.exemplar(x)); x++; } trainTimeElapsed = System.currentTimeMillis() - trainTimeStart; } else if (objectInputFileName.length() == 0) { // Build classifier in one go tempTrain = new Exemplars(train); trainingEvaluation.setPriors(tempTrain); testingEvaluation.setPriors(tempTrain); trainTimeStart = System.currentTimeMillis(); classifier.buildClassifier(tempTrain); trainTimeElapsed = System.currentTimeMillis() - trainTimeStart; } // Save the classifier if an object output file is provided if (objectOutputFileName.length() != 0) { OutputStream os = new FileOutputStream(objectOutputFileName); if (objectOutputFileName.endsWith(".gz")) { os = new GZIPOutputStream(os); } ObjectOutputStream objectOutputStream = new ObjectOutputStream(os); objectOutputStream.writeObject(classifier); objectOutputStream.flush(); objectOutputStream.close(); } // If classifier is drawable output string describing graph if ((classifier instanceof Drawable) && (printGraph)) { return ((Drawable) classifier).graph(); } // Output the classifier as equivalent source if ((classifier instanceof Sourcable) && (printSource)) { return wekaStaticWrapper((Sourcable) classifier, sourceClass); } // Output model if (classifier instanceof OptionHandler) { if (schemeOptionsText != null) { text.append("\nOptions: " + schemeOptionsText); text.append("\n"); } } text.append("\n" + classifier.toString() + "\n"); if (costMatrix != null) { text.append("\n=== Evaluation Cost Matrix ===\n\n").append(costMatrix.toString()); } // Compute error estimate from training data if (trainFileName.length() != 0) { if ((classifier instanceof MIUpdateableClassifier) && (testFileName.length() != 0) && (costMatrix == null)) { // Classifier was trained incrementally, so we have to // reopen the training data in order to test on it. trainReader = new BufferedReader(new FileReader(trainFileName)); // Incremental testing Instances trn = new Instances(trainReader); if (classIndex != -1) { trn.setClassIndex(classIndex - 1); } else { trn.setClassIndex(trn.numAttributes() - 1); } testTimeStart = System.currentTimeMillis(); if (idIndex != -1) train = new Exemplars(trn, idIndex); else train = new Exemplars(trn, 0); for (int y = 0; y < train.numExemplars(); y++) { trainingEvaluation.evaluateModelOnce((MIClassifier) classifier, train.exemplar(y)); } testTimeElapsed = System.currentTimeMillis() - testTimeStart; trainReader.close(); } else { testTimeStart = System.currentTimeMillis(); trainingEvaluation.evaluateModel(classifier, train); testTimeElapsed = System.currentTimeMillis() - testTimeStart; } // Print the results of the training evaluation text.append("\nTime taken to build model: " + Utils.doubleToString(trainTimeElapsed / 1000.0, 2) + " seconds"); text.append("\nTime taken to test model on training data: " + Utils.doubleToString(testTimeElapsed / 1000.0, 2) + " seconds"); text.append(trainingEvaluation.toSummaryString("\n\n=== Error on training" + " data ===\n", printComplexityStatistics)); if (template.classAttribute().isNominal()) { if (classStatistics) { text.append("\n\n" + trainingEvaluation.toClassDetailsString()); } text.append("\n\n" + trainingEvaluation.toMatrixString()); } } // Compute proper error estimates if (testFileName.length() != 0) { // Testing is on the supplied test data for (int z = 0; z < test.numExemplars(); z++) testingEvaluation.evaluateModelOnce((MIClassifier) classifier, test.exemplar(z)); text.append("\n\n" + testingEvaluation.toSummaryString("=== Error on test data ===\n", printComplexityStatistics)); } else if (trainFileName.length() != 0) { // Testing is via cross-validation on training data if (random == null) random = new Random(seed); random.setSeed(seed); // In case exemplars are changed by classifier if (idIndex != -1) train = new Exemplars(data, idIndex); else train = new Exemplars(data, 0); train.randomize(random); testingEvaluation.crossValidateModel(classifier, train, folds); if (leaveOneOut) text.append("\n\n\n" + testingEvaluation.toSummaryString("=== Leave One Out Error ===\n", printComplexityStatistics)); else text.append("\n\n\n" + testingEvaluation .toSummaryString("=== Stratified " + "cross-validation ===\n", printComplexityStatistics)); } if (template.classAttribute().isNominal()) { if (classStatistics) { text.append("\n\n" + testingEvaluation.toClassDetailsString()); } text.append("\n\n" + testingEvaluation.toMatrixString()); } return text.toString(); }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Main method for testing this class -- just prints out a set * of Exemplars. Assume the ID index is 0. * * @param argv should contain one element: the name of an ARFF file *//*from ww w . j a v a 2 s . c om*/ public static void main(String[] args) { try { Reader r = null; if (args.length > 1) { throw (new Exception("Usage: Instances <filename>")); } else if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); } else { r = new BufferedReader(new FileReader(args[0])); } Instances i = new Instances(r); i.setClassIndex(i.numAttributes() - 1); Attribute id = i.attribute(0); if (!id.isNominal()) throw new Exception("The first attribute is not nominal"); Exemplar[] egs = new Exemplar[id.numValues()]; for (int j = 0; j < egs.length; j++) egs[j] = null; for (int j = 0; j < i.numInstances(); j++) { Instance ins = i.instance(j); int idv = (int) ins.value(0); if (egs[idv] == null) egs[idv] = new Exemplar(ins, 0); else egs[idv].add(ins); } for (int j = 0; j < egs.length; j++) System.out.println(egs[j].toString()); } catch (Exception ex) { System.err.println(ex.getMessage()); } }
From source file:milk.core.Exemplars.java
License:Open Source License
/** * Main method for this class -- just performone run of 10-fold CV * and prints out the set. Assume ID is the first attribute and class * is the last one.// ww w .ja v a 2 s. c o m * * @param argv should contain one element: the name of an ARFF file */ public static void main(String[] args) { try { Reader r = null; if (args.length > 1) { throw (new Exception("Usage: Exemplers <filename>")); } else if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); } else { r = new BufferedReader(new FileReader(args[0])); } Instances data = new Instances(r); data.setClassIndex(data.numAttributes() - 1); Exemplars e = new Exemplars(data, 0); System.out.println("\nOriginal whole data:\n" + e.toString()); Exemplars ex = new Exemplars(e); e = new Exemplars(ex, ex.numExemplars()); for (int i = 0; i < ex.numExemplars(); i++) e.add(ex.exemplar(i)); e.stratify(3); System.out.println("\nWhole data after stratification:\n" + e.toString()); e.sort(); System.out.println("\nWhole data after sorting by Exemplar #:\n" + e.toString()); Random ran = new Random(System.currentTimeMillis()); e.randomize(ran); System.out.println("\nWhole data after randomization:\n" + e.toString()); Exemplars egs = e.resample(ran); System.out.println("\nResampled data:\n" + egs.toString()); Exemplars test = e.testCV(10, 1); System.out.println("\nTesting data\n" + test.toString()); Exemplars train = e.trainCV(10, 1); System.out.println("\nTraining data:\n" + train.toString()); } catch (Exception ex) { System.err.println(ex.getMessage()); } }