Java Code Examples for meka.core.Result

The following examples show how to use meka.core.Result. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Test
public void testFitAndPredictWithHoldoutSplitter() throws Exception {
	BR br = new BR();
	br.buildClassifier(splitterSplit.get(0).getInstances());
	Result res = Evaluation.testClassifier(br, splitterSplit.get(1).getInstances());
	double[][] mekaPredictions = res.allPredictions();

	MekaClassifier classifier = new MekaClassifier(new BR());
	classifier.fit(splitterSplit.get(0));
	IMultiLabelClassificationPredictionBatch pred = classifier.predict(splitterSplit.get(1));

	assertEquals("Number of predictions is not consistent.", splitterSplit.get(1).size(), pred.getNumPredictions());

	double[][] jaicorePredictions = pred.getPredictionMatrix();
	assertEquals("Length of prediction matrices is not consistent.", mekaPredictions.length, jaicorePredictions.length);
	assertEquals("Width of prediction matrices is not consistent.", mekaPredictions[0].length, jaicorePredictions[0].length);

	for (int i = 0; i < mekaPredictions.length; i++) {
		for (int j = 0; j < mekaPredictions[i].length; j++) {
			assertEquals("The prediction for instance " + i + " and label " + j + " is not consistent.", mekaPredictions[i][j], jaicorePredictions[i][j], 1E-8);
		}
	}
}
 
Example 2
Source Project: meka   Source File: TrainTestSplit.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 2)
    throw new IllegalArgumentException("Required arguments: <dataset> <percentage>");

  System.out.println("Loading data: " + args[0]);
  Instances data = DataSource.read(args[0]);
  MLUtils.prepareData(data);

  double percentage = Double.parseDouble(args[1]);
  int trainSize = (int) (data.numInstances() * percentage / 100.0);
  Instances train = new Instances(data, 0, trainSize);
  Instances test = new Instances(data, trainSize, data.numInstances() - trainSize);

  System.out.println("Build BR classifier on " + percentage + "%");
  BR classifier = new BR();
  // further configuration of classifier
  classifier.buildClassifier(train);

  System.out.println("Evaluate BR classifier on " + (100.0 - percentage) + "%");
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.evaluateModel(classifier, train, test, top, vop);

  System.out.println(result);
}
 
Example 3
Source Project: meka   Source File: CrossValidate.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 1)
    throw new IllegalArgumentException("Required arguments: <dataset>");

  System.out.println("Loading data: " + args[0]);
  Instances data = DataSource.read(args[0]);
  MLUtils.prepareData(data);

  int numFolds = 10;
  System.out.println("Cross-validate BR classifier using " + numFolds + " folds");
  BR classifier = new BR();
  // further configuration of classifier
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.cvModel(classifier, data, numFolds, top, vop);

  System.out.println(result);
}
 
Example 4
Source Project: meka   Source File: ShowMacroCurve.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);

	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			JDialog dialog = new JDialog((Frame) null, history.getSuffixAt(index), false);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.getContentPane().setLayout(new BorderLayout());
			Instances performance = (Instances) result.getMeasurement(CURVE_DATA_MACRO);
			try {
				VisualizePanel panel = createPanel(performance);
				dialog.getContentPane().add(panel, BorderLayout.CENTER);
			}
			catch (Exception ex) {
				System.err.println("Failed to create plot!");
				ex.printStackTrace();
			}
			dialog.setSize(800, 600);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
		}
	};
}
 
Example 5
Source Project: meka   Source File: IncrementalPerformance.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);

	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			JDialog dialog = new JDialog((Frame) null, history.getSuffixAt(index), false);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.getContentPane().setLayout(new BorderLayout());
			Instances performance = (Instances) result.getMeasurement(RESULTS_SAMPLED_OVER_TIME);
			try {
				VisualizePanel panel = createPanel(performance);
				dialog.getContentPane().add(panel, BorderLayout.CENTER);
			}
			catch (Exception ex) {
				System.err.println("Failed to create plot!");
				ex.printStackTrace();
			}
			dialog.setSize(800, 600);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
		}
	};
}
 
Example 6
Source Project: meka   Source File: ShowMicroCurve.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);

	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			JDialog dialog = new JDialog((Frame) null, history.getSuffixAt(index), false);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.getContentPane().setLayout(new BorderLayout());
			Instances performance = (Instances) result.getMeasurement(CURVE_DATA_MICRO);
			try {
				VisualizePanel panel = createPanel(performance);
				dialog.getContentPane().add(panel, BorderLayout.CENTER);
			}
			catch (Exception ex) {
				System.err.println("Failed to create plot!");
				ex.printStackTrace();
			}
			dialog.setSize(800, 600);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
		}
	};
}
 
Example 7
/**
 * Turns the results from the regression test into a string.
 *
 * @param result        the result to process
 * @return              the generated string
 */
protected String postProcessRegressionResults(Result result) {
	StringBuilder   processed;
	String[]        lines;

	processed = new StringBuilder();
	lines     = result.toString().split("\n");
	for (String line: lines) {
		if (line.toLowerCase().contains("time"))
			continue;
		if (processed.length() > 0)
			processed.append("\n");
		processed.append(line);
	}

	return processed.toString();
}
 
Example 8
Source Project: meka   Source File: DeepMethodsTests.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void testDeepML() {
	System.out.println("Test Stacked Boltzmann Machines with an off-the-shelf multi-label classifier");
	DeepML dbn = new DeepML();

	MCC h = new MCC();
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);

	dbn.setClassifier(h);
	dbn.setE(100);
	dbn.setH(30);

	Result r = EvaluationTests.cvEvaluateClassifier(dbn);
	System.out.println("DeepML + MCC" + r.getMeasurement("Accuracy"));
	String s = (String)r.getMeasurement("Accuracy");
	assertTrue("DeepML+MCC Accuracy Correct", s.startsWith("0.53")); // Good enough 
}
 
Example 9
Source Project: meka   Source File: DeepMethodsTests.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void testDBPNN() {
	
	System.out.println("Test Back Prop Neural Network with pre-trained weights (via RBM)");
	DBPNN dbn = new DBPNN();
	dbn.setClassifier(new BPNN());
	dbn.setDebug(true);

	try {
		dbn.setOptions(Utils.splitOptions("-H 30 -E 500 -r 0.1 -m 0.2"));
	} catch(Exception e) {
		System.err.println("Fatal Error");
		e.printStackTrace();
		System.exit(1);
	}
	Result r = EvaluationTests.cvEvaluateClassifier(dbn);
	String s = (String)r.getMeasurement("Accuracy");
	System.out.println("DBPNN + _" + r.getMeasurement("Accuracy"));
	assertTrue("DBPNN Accuracy Correct", s.equals("0.556 +/- 0.038"));
}
 
Example 10
Source Project: meka   Source File: EvaluationTests.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void testRepeatable() {
	// Load Music
	Instances D = loadInstances("Music.arff");
	Instances D_train = new Instances(D,0,400);
	Instances D_test = new Instances(D,400,D.numInstances()-400);
	// Train ECC
	MultiLabelClassifier h = makeECC();
	// Eval
	try {
		Result r1 = Evaluation.evaluateModel(h, D_train, D_test, "PCut1");
		Result r2 = Evaluation.evaluateModel(h, D_train, D_test, "PCut1");
		assertTrue("Experiments are Repeatable (with same result)", r1.getMeasurement("Accuracy").equals(r2.getMeasurement("Accuracy")));
	} catch(Exception e) {
		e.printStackTrace();
	}
}
 
Example 11
Source Project: meka   Source File: MiscMethodsTests.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void testMULAN() {
	// Test MULAN
	System.out.println("Test MULAN");
	MULAN mulan = new MULAN();
	// ... RAkEL
	mulan.setMethod("RAkEL2");
	mulan.setClassifier(new SMO());
	Result r;
	r = EvaluationTests.cvEvaluateClassifier(mulan);
	System.out.println("MULAN (RAkEL): "+r.output.get("Accuracy"));
	assertTrue("MULAN (RAkEL) Accuracy Correct", ((String)r.output.get("Accuracy")).startsWith("0.58") );
	// ... MLkNN
	mulan.setMethod("MLkNN");
	r = EvaluationTests.cvEvaluateClassifier(mulan);
	System.out.println("MULAN (MLkNN): "+r.output.get("Accuracy"));
	assertTrue("MULAN (MLkNN) Accuracy Correct", r.output.get("Accuracy").equals("0.561 +/- 0.035") );
	// ... BR (and , vs MEKA's BR)
	mulan.setMethod("BR");
	r = EvaluationTests.cvEvaluateClassifier(mulan);
	System.out.println("MULAN (BR): "+r.output.get("Accuracy"));
	assertTrue("MULAN (BR) Accuracy Correct", r.output.get("Accuracy").equals("0.493 +/- 0.036") );
	BR br = new BR();
	br.setClassifier(new SMO());
	Result r_other = EvaluationTests.cvEvaluateClassifier(br);
	assertTrue("MULAN BR Equal to MEKA BR", r.output.get("Accuracy").equals(r_other.output.get("Accuracy")) );
}
 
Example 12
Source Project: AILibs   Source File: MLPlanCLI.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
private static void writeMultiLabelEvaluationFile(final Result result, final double internalError, final CommandLine commandLine, final Classifier bestModel) {
	StringBuilder builder = new StringBuilder();
	builder.append("Internally believed error: ");
	builder.append(internalError);
	builder.append(System.lineSeparator());
	builder.append(System.lineSeparator());
	builder.append("Best Model: ");
	builder.append(System.lineSeparator());
	builder.append(bestModel.toString());
	builder.append(System.lineSeparator());
	builder.append(System.lineSeparator());
	builder.append(result.toString());
	builder.append(System.lineSeparator());
	builder.append(System.lineSeparator());
	if (commandLine.hasOption(printModelOption)) {
		builder.append("Classifier Representation: ");
		builder.append(System.lineSeparator());
		builder.append(System.lineSeparator());
		if (bestModel instanceof ai.libs.jaicore.ml.weka.classification.pipeline.MLPipeline) {
			builder.append(((MLPipeline) bestModel).getBaseClassifier().toString());
		} else {
			builder.append(bestModel.toString());
		}
	}

	writeFile(commandLine.getOptionValue(resultsFileOption, resultsFile), builder.toString());
}
 
Example 13
public static void main(String[] args) throws Exception {
  if (args.length != 3)
    throw new IllegalArgumentException("Required arguments: <train> <test> <output>");

  System.out.println("Loading train: " + args[0]);
  Instances train = DataSource.read(args[0]);
  MLUtils.prepareData(train);

  System.out.println("Loading test: " + args[1]);
  Instances test = DataSource.read(args[1]);
  MLUtils.prepareData(test);

  // compatible?
  String msg = train.equalHeadersMsg(test);
  if (msg != null)
    throw new IllegalStateException(msg);

  System.out.println("Build BR classifier on " + args[0]);
  BR classifier = new BR();
  // further configuration of classifier
  classifier.buildClassifier(train);

  System.out.println("Evaluate BR classifier on " + args[1]);
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.evaluateModel(classifier, train, test, top, vop);

  System.out.println(result);

  System.out.println("Saving predictions test set to " + args[2]);
  Instances performance = Result.getPredictionsAsInstances(result);
  DataSink.write(args[2], performance);
}
 
Example 14
Source Project: meka   Source File: TrainTestSet.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 2)
    throw new IllegalArgumentException("Required arguments: <train> <test>");

  System.out.println("Loading train: " + args[0]);
  Instances train = DataSource.read(args[0]);
  MLUtils.prepareData(train);

  System.out.println("Loading test: " + args[1]);
  Instances test = DataSource.read(args[1]);
  MLUtils.prepareData(test);

  // compatible?
  String msg = train.equalHeadersMsg(test);
  if (msg != null)
    throw new IllegalStateException(msg);

  System.out.println("Build BR classifier on " + args[0]);
  BR classifier = new BR();
  // further configuration of classifier
  classifier.buildClassifier(train);

  System.out.println("Evaluate BR classifier on " + args[1]);
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.evaluateModel(classifier, train, test, top, vop);

  System.out.println(result);
}
 
Example 15
Source Project: meka   Source File: MicroCurve.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 1)
    throw new IllegalArgumentException("Required arguments: <dataset>");

  System.out.println("Loading data: " + args[0]);
  Instances data = DataSource.read(args[0]);
  MLUtils.prepareData(data);

  System.out.println("Cross-validate BR classifier");
  BR classifier = new BR();
  // further configuration of classifier
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.cvModel(classifier, data, 10, top, vop);

  JFrame frame = new JFrame("Micro curve");
  frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(new BorderLayout());
  Instances performance = (Instances) result.getMeasurement(CURVE_DATA_MICRO);
  try {
    VisualizePanel panel = createPanel(performance);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
  }
  catch (Exception ex) {
    System.err.println("Failed to create plot!");
    ex.printStackTrace();
  }
  frame.setSize(800, 600);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
}
 
Example 16
Source Project: meka   Source File: ROC.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 1)
    throw new IllegalArgumentException("Required arguments: <dataset>");

  System.out.println("Loading data: " + args[0]);
  Instances data = DataSource.read(args[0]);
  MLUtils.prepareData(data);

  System.out.println("Cross-validate BR classifier");
  BR classifier = new BR();
  // further configuration of classifier
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.cvModel(classifier, data, 10, top, vop);

  JFrame frame = new JFrame("ROC");
  frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(new BorderLayout());
  JTabbedPane tabbed = new JTabbedPane();
  frame.getContentPane().add(tabbed, BorderLayout.CENTER);
  Instances[] curves = (Instances[]) result.getMeasurement(CURVE_DATA);
  for (int i = 0; i < curves.length; i++) {
    try {
      ThresholdVisualizePanel panel = createPanel(curves[i], "Label " + i);
      tabbed.addTab("" + i, panel);
    }
    catch (Exception ex) {
      System.err.println("Failed to create plot for label " + i);
      ex.printStackTrace();
    }
  }
  frame.setSize(800, 600);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
}
 
Example 17
Source Project: meka   Source File: MacroCurve.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 1)
    throw new IllegalArgumentException("Required arguments: <dataset>");

  System.out.println("Loading data: " + args[0]);
  Instances data = DataSource.read(args[0]);
  MLUtils.prepareData(data);

  System.out.println("Cross-validate BR classifier");
  BR classifier = new BR();
  // further configuration of classifier
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.cvModel(classifier, data, 10, top, vop);

  JFrame frame = new JFrame("Macro curve");
  frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(new BorderLayout());
  Instances performance = (Instances) result.getMeasurement(CURVE_DATA_MACRO);
  try {
    VisualizePanel panel = createPanel(performance);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
  }
  catch (Exception ex) {
    System.err.println("Failed to create plot!");
    ex.printStackTrace();
  }
  frame.setSize(800, 600);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
}
 
Example 18
Source Project: meka   Source File: PrecisionRecall.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 1)
    throw new IllegalArgumentException("Required arguments: <dataset>");

  System.out.println("Loading data: " + args[0]);
  Instances data = DataSource.read(args[0]);
  MLUtils.prepareData(data);

  System.out.println("Cross-validate BR classifier");
  BR classifier = new BR();
  // further configuration of classifier
  String top = "PCut1";
  String vop = "3";
  Result result = Evaluation.cvModel(classifier, data, 10, top, vop);

  JFrame frame = new JFrame("Precision-recall");
  frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(new BorderLayout());
  JTabbedPane tabbed = new JTabbedPane();
  frame.getContentPane().add(tabbed, BorderLayout.CENTER);
  Instances[] curves = (Instances[]) result.getMeasurement(CURVE_DATA);
  for (int i = 0; i < curves.length; i++) {
    try {
      ThresholdVisualizePanel panel = createPanel(curves[i], "Label " + i);
      tabbed.addTab("" + i, panel);
    }
    catch (Exception ex) {
      System.err.println("Failed to create plot for label " + i);
      ex.printStackTrace();
    }
  }
  frame.setSize(800, 600);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
}
 
Example 19
Source Project: meka   Source File: Evaluation.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * EvaluateModel - Build model 'h' on 'D_train', test it on 'D_test', threshold it according to 'top', verbosity 'vop'.
 * @param	h		a multi-dim. classifier
 * @param	D_train	training data
 * @param	D_test 	test data
 * @param	top    	Threshold OPtion (pertains to multi-label data only)
 * @param	vop    	Verbosity OPtion (which measures do we want to calculate/output)
 * @return	Result	raw prediction data with evaluation statistics included.
 */
public static Result evaluateModel(MultiXClassifier h, Instances D_train, Instances D_test, String top, String vop) throws Exception {
	Result r = evaluateModel(h,D_train,D_test);
	if (h instanceof MultiTargetClassifier || isMT(D_test)) {
		r.setInfo("Type","MT");
	}
	else if (h instanceof MultiLabelClassifier) {
		r.setInfo("Type","ML");
		r.setInfo("Threshold",MLEvalUtils.getThreshold(r.predictions,D_train,top)); // <-- only relevant to ML (for now), but we'll put it in here in any case
	}
	r.setInfo("Verbosity",vop);
	r.output = Result.getStats(r, vop);
	return r;
}
 
Example 20
Source Project: meka   Source File: Evaluation.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * EvaluateModel - Assume 'h' is already built, test it on 'D_test', threshold it according to 'top', verbosity 'vop'.
 * @param	h		a multi-dim. classifier
 * @param	D_test 	test data
 * @param	tal    	Threshold VALUES (not option)
 * @param	vop    	Verbosity OPtion (which measures do we want to calculate/output)
 * @return	Result	raw prediction data with evaluation statistics included.
 */
public static Result evaluateModel(MultiXClassifier h, Instances D_test, String tal, String vop) throws Exception {
	Result r = testClassifier(h,D_test);
	if (h instanceof MultiTargetClassifier || isMT(D_test)) {
		r.setInfo("Type","MT");
	}
	else if (h instanceof MultiLabelClassifier) {
		r.setInfo("Type","ML");
	}
	r.setInfo("Threshold",tal);
	r.setInfo("Verbosity",vop);
	r.output = Result.getStats(r, vop);
	return r;
}
 
Example 21
Source Project: meka   Source File: Evaluation.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * CVModel - Split D into train/test folds, and then train and evaluate on each one.
 * @param	h		 a multi-output classifier
 * @param	D      	 test data Instances
 * @param	numFolds number of folds of CV
 * @param	top    	 Threshold OPtion (pertains to multi-label data only)
 * @param	vop    	Verbosity OPtion (which measures do we want to calculate/output)
 * @return	Result	raw prediction data with evaluation statistics included.
 */
public static Result cvModel(MultiLabelClassifier h, Instances D, int numFolds, String top, String vop) throws Exception {
	Result r_[] = new Result[numFolds];
	for(int i = 0; i < numFolds; i++) {
		Instances D_train = D.trainCV(numFolds,i);
		Instances D_test = D.testCV(numFolds,i);
		if (h.getDebug()) System.out.println(":- Fold ["+i+"/"+numFolds+"] -: "+MLUtils.getDatasetName(D)+"\tL="+D.classIndex()+"\tD(t:T)=("+D_train.numInstances()+":"+D_test.numInstances()+")\tLC(t:T)="+Utils.roundDouble(MLUtils.labelCardinality(D_train,D.classIndex()),2)+":"+Utils.roundDouble(MLUtils.labelCardinality(D_test,D.classIndex()),2)+")");
		r_[i] = evaluateModel(h, D_train, D_test); // <-- should not run stats yet!
	}
	Result r = MLEvalUtils.combinePredictions(r_);
	if (h instanceof MultiTargetClassifier || isMT(D)) {
		r.setInfo("Type","MT-CV");
	}
	else if (h instanceof MultiLabelClassifier) {
		r.setInfo("Type","ML-CV");
		try {
			r.setInfo("Threshold",String.valueOf(Double.parseDouble(top)));
		} catch(Exception e) {
			System.err.println("[WARNING] Automatic threshold calibration not currently enabled for cross-fold validation, setting threshold = 0.5.\n");
			r.setInfo("Threshold",String.valueOf(0.5));
		}
	}
	r.setInfo("Verbosity",vop);
	r.output = Result.getStats(r, vop);
	// Need to reset this because of CV
	r.setValue("Number of training instances",D.numInstances());
	r.setValue("Number of test instances",D.numInstances());
	return r;
}
 
Example 22
Source Project: meka   Source File: Evaluation.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * TestClassifier - test classifier h on D_test
 * @param	h		a multi-dim. classifier, ALREADY BUILT
 * @param	D_test 	test data
 * @return	Result	with raw prediction data ONLY
 */
public static Result testClassifier(MultiXClassifier h, Instances D_test) throws Exception {

	int L = D_test.classIndex();
	Result result = new Result(D_test.numInstances(),L);

	if(h.getDebug()) System.out.print(":- Evaluate ");
	for (int i = 0, c = 0; i < D_test.numInstances(); i++) {

		if(h.getDebug()) { int t = i*50/D_test.numInstances(); if(t > c) { System.out.print("#"); c = t; } }

		// No cheating allowed; clear all class information
		Instance x = (Instance)(D_test.instance(i)).copy();
		for(int v = 0; v < D_test.classIndex(); v++)
			x.setValue(v,0.0);

		// Get and store ranking
		double y[] = h.distributionForInstance(x);
		// Cut off any [no-longer-needed] probabalistic information from MT classifiers.
		if (h instanceof MultiTargetClassifier)
                           y = Arrays.copyOfRange(y, L, L*2);

		// Store the result
		result.addResult(y,D_test.instance(i));
	}
	if(h.getDebug()) System.out.println(":-");

	/*
	if(h.getDebug()) {

		for(int i = 0; i < result.size(); i++) {
			System.out.println("\t"+Arrays.toString(result.rowTrue(i))+" vs "+Arrays.toString(result.rowRanking(i)));
		}
	}
	*/

	return result;
}
 
Example 23
Source Project: meka   Source File: Evaluation.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 *Test Classifier but threaded (Multiple)
 * @param	h		a multi-dim. classifier, ALREADY BUILT (threaded, implements MultiLabelThreaded)
 * @param	D_test 	test data
 * @return	Result	with raw prediction data ONLY
 */
public static Result testClassifierM(MultiXClassifier h, Instances D_test) throws Exception {

	int L = D_test.classIndex();
	Result result = new Result(D_test.numInstances(),L);
	if(h.getDebug()) System.out.print(":- Evaluate ");
	if(h instanceof MultiLabelClassifierThreaded){
		((MultiLabelClassifierThreaded)h).setThreaded(true);
		double y[][] = ((MultiLabelClassifierThreaded)h).distributionForInstanceM(D_test);

		for (int i = 0, c = 0; i < D_test.numInstances(); i++) {
			// Store the result
			result.addResult(y[i],D_test.instance(i));
		}
		if(h.getDebug()) System.out.println(":-");

	/*
	if(h.getDebug()) {

		for(int i = 0; i < result.size(); i++) {
			System.out.println("\t"+Arrays.toString(result.rowActual(i))+" vs "+Arrays.toString(result.rowRanking(i)));
		}


	}
	*/
	}
	return result;
}
 
Example 24
Source Project: meka   Source File: SaveCSV.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);

	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			Instances performance = (Instances) result.getMeasurement(IncrementalPerformance.RESULTS_SAMPLED_OVER_TIME);

			int retVal = getFileChooser().showSaveDialog(null);
			if (retVal != MekaFileChooser.APPROVE_OPTION)
				return;
			File file = getFileChooser().getSelectedFile();


			try {

				CSVSaver saver = new CSVSaver();
				saver.setInstances(performance);
				saver.setFile(getFileChooser().getSelectedFile());
				saver.writeBatch();
			} catch (Exception ex) {
				String msg = "Failed to write to '" + file + "'!";
				System.err.println(msg);
				ex.printStackTrace();
				JOptionPane.showMessageDialog( null, msg + "\n" + e);
			}
		}
	};
}
 
Example 25
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);

	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			JDialog dialog = new JDialog((Frame) null, history.getSuffixAt(index), false);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			JTabbedPane tabbed = new JTabbedPane();
			dialog.getContentPane().setLayout(new BorderLayout());
			dialog.getContentPane().add(tabbed, BorderLayout.CENTER);
			Instances[] curves = (Instances[]) result.getMeasurement(CURVE_DATA);
			for (int i = 0; i < curves.length; i++) {
				try {
					ThresholdVisualizePanel panel = createPanel(curves[i], "Label " + i);
					tabbed.addTab("" + i, panel);
				}
				catch (Exception ex) {
					System.err.println("Failed to create plot for label " + i);
					ex.printStackTrace();
				}
			}
			dialog.setSize(800, 600);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
		}
	};
}
 
Example 26
Source Project: meka   Source File: SavePredictions.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);
	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			int retVal = getFileChooser().showSaveDialog(null);
			if (retVal != MekaFileChooser.APPROVE_OPTION)
				return;
			File file = getFileChooser().getSelectedFile();

			try {

				CSVSaver saver = new CSVSaver();
				Instances performance = Result.getPredictionsAsInstances(result);
				saver.setInstances(performance);
				saver.setFile(getFileChooser().getSelectedFile());
				saver.writeBatch();
			} catch (Exception ex) {
				String msg = "Failed to write to '" + file + "'!";
				System.err.println(msg);
				ex.printStackTrace();
				JOptionPane.showMessageDialog( null, msg + "\n" + e);
			}
		}
	};
}
 
Example 27
Source Project: meka   Source File: ClassifyTab.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds the result to the history.
 *
 * @param result the result to add
 * @param payload the payload to add
 * @param suffix the suffix to add
 */
public void addResultToHistory(final Result result, final Object payload, final String suffix) {
	Runnable run;

	run = new Runnable() {
		@Override
		public void run() {
			m_ResultHistoryList.addResult(result, payload, suffix);
		}
	};
	SwingUtilities.invokeLater(run);
}
 
Example 28
Source Project: meka   Source File: ClassifyTab.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Displays the selected results.
 */
protected void displayResults() {
	Result result;

	if (m_ResultHistoryList.getSelectedIndex() == -1) {
		m_TextAreaResults.setText("");
		return;
	}

	result = m_ResultHistoryList.getResultAt(m_ResultHistoryList.getSelectedIndex());
	if (result == null)
		return;

	m_TextAreaResults.setText(result.toString());
}
 
Example 29
Source Project: meka   Source File: ResultHistory.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initializes the history.
 */
public ResultHistory() {
	m_Results  = new Hashtable<Date,Result>();
	m_Payloads = new Hashtable<Date,Object>();
	m_Suffixes = new Hashtable<Date,String>();
	m_Ordered  = new ArrayList<Date>();
}
 
Example 30
Source Project: meka   Source File: ResultHistory.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes the specified entry.
 * 
 * @param index the history entry to remove
 * @return the removed item
 */
public synchronized Result remove(int index) {
	Result result;
	Date date;
	
	date   = m_Ordered.remove(index);
	m_Suffixes.remove(date);
	m_Payloads.remove(date);
	result = m_Results.remove(date);
	
	return result;
}