Java Code Examples for weka.core.Instances#equalHeadersMsg()

The following examples show how to use weka.core.Instances#equalHeadersMsg() . 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 check out the related API usage on the sidebar.
Example 1
Source File: CheckEstimator.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Compare two datasets to see if they differ.
 *
 * @param data1 one set of instances
 * @param data2 the other set of instances
 * @throws Exception if the datasets differ
 */
protected void compareDatasets(Instances data1, Instances data2)
throws Exception {
  if (!data2.equalHeaders(data1)) {
    throw new Exception("header has been modified\n" + data2.equalHeadersMsg(data1));
  }
  if (!(data2.numInstances() == data1.numInstances())) {
    throw new Exception("number of instances has changed");
  }
  for (int i = 0; i < data2.numInstances(); i++) {
    Instance orig = data1.instance(i);
    Instance copy = data2.instance(i);
    for (int j = 0; j < orig.numAttributes(); j++) {
      if (orig.isMissing(j)) {
        if (!copy.isMissing(j)) {
          throw new Exception("instances have changed");
        }
      } else if (orig.value(j) != copy.value(j)) {
        throw new Exception("instances have changed");
      }
      if (orig.weight() != copy.weight()) {
        throw new Exception("instance weights have changed");
      }	  
    }
  }
}
 
Example 2
Source File: MekaSearch.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Loads test data, if required.
 *
 * @param data	the current training data
 * @throws Exception	if test sets are not compatible with training data
 */
protected void loadTestData(Instances data) throws Exception {
	String		msg;

	m_InitialSpaceTestInst = null;
	if (m_InitialSpaceTestSet.exists() && !m_InitialSpaceTestSet.isDirectory()) {
		m_InitialSpaceTestInst = DataSource.read(m_InitialSpaceTestSet.getAbsolutePath());
		m_InitialSpaceTestInst.setClassIndex(data.classIndex());
		msg = data.equalHeadersMsg(m_InitialSpaceTestInst);
		if (msg != null)
			throw new IllegalArgumentException("Test set for initial space not compatible with training dta:\n" +  msg);
		m_InitialSpaceTestInst.deleteWithMissingClass();
		log("Using test set for initial space: " + m_InitialSpaceTestSet);
	}

	m_SubsequentSpaceTestInst = null;
	if (m_SubsequentSpaceTestSet.exists() && !m_SubsequentSpaceTestSet.isDirectory()) {
		m_SubsequentSpaceTestInst = DataSource.read(m_SubsequentSpaceTestSet.getAbsolutePath());
		m_SubsequentSpaceTestInst.setClassIndex(data.classIndex());
		msg = data.equalHeadersMsg(m_SubsequentSpaceTestInst);
		if (msg != null)
			throw new IllegalArgumentException("Test set for subsequent sub-spaces not compatible with training dta:\n" +  msg);
		m_SubsequentSpaceTestInst.deleteWithMissingClass();
		log("Using test set for subsequent sub-spaces: " + m_InitialSpaceTestSet);
	}
}
 
Example 3
Source File: CollectiveClassifierPanel.java    From collective-classification-weka-package with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Evaluates the classifier on a test set.
  *
  * @param classifier the classifier to evaluate
  * @param output for collecting the test results
  * @param outBuff the buffer for the output
  * @return the generated evaluation
  * @throws Execption if evaluation fails
  */
 protected CollectiveEvaluation evalTestSet(CollectiveClassifier classifier, AbstractOutput output, StringBuffer outBuff) throws Exception {
   CollectiveEvaluation eval;

   if (m_UnlabeledSet == null)
     throw new IllegalStateException("No unlabeled dataset set!");
   Instances train = new Instances(m_Instances);
   train.setClassIndex(m_ClassCombo.getSelectedIndex());
   Instances unlabeled = new Instances(m_UnlabeledSet);
   unlabeled.setClassIndex(m_ClassCombo.getSelectedIndex());
   if (!train.equalHeaders(unlabeled))
     throw new IllegalStateException(train.equalHeadersMsg(unlabeled));
   eval = new CollectiveEvaluation(train);
   classifier.buildClassifier(train, unlabeled);
   if (m_TestSet != null) {
     Instances test = new Instances(m_TestSet);
     test.setClassIndex(m_ClassCombo.getSelectedIndex());
     if (!train.equalHeaders(test))
       throw new IllegalStateException(train.equalHeadersMsg(test));
     if (output != null) {
       printPredictionsHeader(outBuff, output, "test set");
       output.printHeader();
       eval.evaluateModel(classifier, test, output);
output.printFooter();
     }
     else {
       eval.evaluateModel(classifier, test);
     }
   }

   return eval;
 }
 
Example 4
Source File: ExportPredictionsOnTestSet.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
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 5
Source File: TrainTestSet.java    From meka with 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 6
Source File: TrainAndPredict.java    From meka with 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> <predict>");

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

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

  // compatible?
  String msg = train.equalHeadersMsg(predict);
  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("Use BR classifier on " + args[1]);
  for (int i = 0; i < predict.numInstances(); i++) {
    double[] dist = classifier.distributionForInstance(predict.instance(i));
    System.out.println((i+1) + ": " + Utils.arrayToString(dist));
  }
}