weka.core.converters.ConverterUtils.DataSource Java Examples

The following examples show how to use weka.core.converters.ConverterUtils.DataSource. 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: TestWekaBayes.java    From Java-Data-Analysis with MIT License 8 votes vote down vote up
public static void main(String[] args) throws Exception {
//        ConverterUtils.DataSource source = new ConverterUtils.DataSource("data/AnonFruit.arff");
        DataSource source = new DataSource("data/AnonFruit.arff");
        Instances train = source.getDataSet();
        train.setClassIndex(3);  // target attribute: (Sweet)
        //build model
        NaiveBayes model=new NaiveBayes();
        model.buildClassifier(train);

        //use
        Instances test = train;
        Evaluation eval = new Evaluation(test);
        eval.evaluateModel(model,test);
        List <Prediction> predictions = eval.predictions();
        int k = 0;
        for (Instance instance : test) {
            double actual = instance.classValue();
            double prediction = eval.evaluateModelOnce(model, instance);
            System.out.printf("%2d.%4.0f%4.0f", ++k, actual, prediction);
            System.out.println(prediction != actual? " *": "");
        }
    }
 
Example #2
Source File: SaveModel.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 7 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try {
        DataSource src = new DataSource("/Users/admin/Documents/NetBeansProjects/SaveModel/segment-challenge.arff");
        Instances dt = src.getDataSet();
        dt.setClassIndex(dt.numAttributes() - 1);

        String[] options = new String[4];
        options[0] = "-C";
        options[1] = "0.1";
        options[2] = "-M";
        options[3] = "2";
        J48 mytree = new J48();
        mytree.setOptions(options);
        mytree.buildClassifier(dt);
        
        weka.core.SerializationHelper.write("/Users/admin/Documents/NetBeansProjects/SaveModel/myDT.model", mytree);
    }
    catch (Exception e) {
        System.out.println("Error!!!!\n" + e.getMessage());
    }
}
 
Example #3
Source File: FilterAttribute.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 7 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try{
        DataSource src = new DataSource("/Users/admin/Documents/NetBeansProjects/Datasets/weather.arff");
        Instances dt = src.getDataSet();
        
        String[] op = new String[]{"-R","2-4"};
        Remove rmv = new Remove();
        rmv.setOptions(op);
        rmv.setInputFormat(dt);
        Instances nd = Filter.useFilter(dt, rmv);
        
        ArffSaver s = new ArffSaver();
        s.setInstances(nd);
        s.setFile(new File("fw.arff"));
        s.writeBatch();
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
}
 
Example #4
Source File: ClusterEval.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 6 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try{
        DataSource src = new DataSource("/Users/admin/Documents/NetBeansProjects/ClusterEval/weather.arff");
        Instances dt = src.getDataSet();
        SimpleKMeans model = new SimpleKMeans();
        model.setNumClusters(3);
        model.buildClusterer(dt);
        System.out.println(model);
        
        ClusterEvaluation eval = new ClusterEvaluation();
        DataSource src1 = new DataSource("/Users/admin/Documents/NetBeansProjects/ClusterEval/weather.test.arff");
        Instances tdt = src1.getDataSet();
        eval.setClusterer(model);
        eval.evaluateClusterer(tdt);
        
        System.out.println(eval.clusterResultsToString());
        System.out.println("# of clusters: " + eval.getNumClusters());
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}
 
Example #5
Source File: TrainTestSplit.java    From meka with 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 #6
Source File: InversePowerLawExtrapolationTester.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
private LearningCurveExtrapolator createExtrapolationMethod(final int[] xValues) throws Exception {
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	try {
		DataSetDescription description = client.dataGet(42);
		File file = client.datasetGet(description);
		DataSource source = new DataSource(file.getCanonicalPath());
		dataset = source.getDataSet();
		dataset.setClassIndex(dataset.numAttributes() - 1);
		Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
		dataset.setClassIndex(targetAttribute.index());
	} catch (Exception e) {
		throw new IOException("Could not load data set from OpenML!", e);
	}

	return new LearningCurveExtrapolator(new InversePowerLawExtrapolationMethod(), new WekaClassifier(new J48()), new WekaInstances(dataset), 0.7d, xValues, new SimpleRandomSamplingFactory<>(), 1l);
}
 
Example #7
Source File: LearningCurveExtrapolationEvaluationTester.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testClassifierEvaluationWithLearningCurveExtrapolation() throws Exception {
	// Load dataset from OpenML
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	DataSetDescription description = client.dataGet(42);
	File file = client.datasetGet(description);
	DataSource source = new DataSource(file.getCanonicalPath());
	dataset = source.getDataSet();
	dataset.setClassIndex(dataset.numAttributes() - 1);
	Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
	dataset.setClassIndex(targetAttribute.index());

	// Test classifier evaluation by learning curve extrapolation
	LearningCurveExtrapolationEvaluator evaluator = new LearningCurveExtrapolationEvaluator(new int[] { 8, 16, 64, 128 }, new SystematicSamplingFactory<>(), new WekaInstances(dataset),
			0.8d, new InversePowerLawExtrapolationMethod(), 123l);
	double evaluationResult = evaluator.evaluate(new WekaClassifier(new SMO()));
	Assert.assertTrue(evaluationResult > 0 && evaluationResult <= 100);
}
 
Example #8
Source File: ExtrapolatedSaturationPointEvaluationTester.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testClassifierEvaluationAtSaturationPoint() throws Exception {
	// Load dataset from OpenML and create stratified split
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	DataSetDescription description = client.dataGet(42);
	File file = client.datasetGet(description);
	DataSource source = new DataSource(file.getCanonicalPath());
	dataset = source.getDataSet();
	dataset.setClassIndex(dataset.numAttributes() - 1);
	Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
	dataset.setClassIndex(targetAttribute.index());
	this.createSplit(new WekaInstances(dataset), 0.8, 123l);

	// Test classifier evaluation at saturation point
	ExtrapolatedSaturationPointEvaluator evaluator = new ExtrapolatedSaturationPointEvaluator(new int[] { 8, 16, 64, 128 }, new SystematicSamplingFactory<>(), this.train, 0.7,
			new InversePowerLawExtrapolationMethod(), 123l, this.test, EClassificationPerformanceMeasure.ERRORRATE);
	evaluator.setEpsilon(0.0005d);
	double evaluationResult = evaluator.evaluate(new WekaClassifier(new SMO()));
	Assert.assertTrue(evaluationResult > 0 && evaluationResult <= 100);
}
 
Example #9
Source File: CrossValidate.java    From meka with 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 #10
Source File: MicroCurve.java    From meka with 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);

  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 #11
Source File: Clustering.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 6 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try{
        DataSource src = new DataSource("/Users/admin/Documents/NetBeansProjects/Clustering/weather.arff");
        Instances dt = src.getDataSet();
        SimpleKMeans model = new SimpleKMeans();
        model.setNumClusters(3);
        model.buildClusterer(dt);
        System.out.println(model);
        
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
}
 
Example #12
Source File: AttribSelect.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 6 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try {
          DataSource src = new DataSource("/Users/admin/Documents/NetBeansProjects/Datasets/weather.arff");
          Instances dt = src.getDataSet();
          
          AttributeSelection asel = new AttributeSelection();
          
          CfsSubsetEval evl = new CfsSubsetEval();
          GreedyStepwise sh = new GreedyStepwise();
          
          asel.setEvaluator(evl);
          asel.setSearch(sh);
          asel.setInputFormat(dt);
          
          Instances nd = Filter.useFilter(dt, asel);
          ArffSaver as = new ArffSaver();
          as.setInstances(nd);
          as.setFile(new File("weather-sel.arff"));
          as.writeBatch();
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
}
 
Example #13
Source File: PrepareClassAttributes.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 3)
    throw new IllegalArgumentException("Required parameters: <input> <attribute_indices> <output>");

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

  System.out.println("Applying filter using indices: " + args[1]);
  MekaClassAttributes filter = new MekaClassAttributes();
  filter.setAttributeIndices(args[1]);
  filter.setInputFormat(input);
  Instances output = Filter.useFilter(input, filter);

  System.out.println("Saving filtered data to: " + args[2]);
  ArffSaver saver = new ArffSaver();
  saver.setFile(new File(args[2]));
  DataSink.write(saver, output);
}
 
Example #14
Source File: Trainer.java    From sentiment-analysis with Apache License 2.0 6 votes vote down vote up
/**Returns the Combined (text+POS) Representations.*/
private Instances getComplex(String fileComplex) throws Exception{
	DataSource ds = new DataSource(fileComplex);
	Instances data =  ds.getDataSet();
	data.setClassIndex(1);
	StringToWordVector filter = new StringToWordVector();
	filter.setInputFormat(data);
	filter.setLowerCaseTokens(true);
	filter.setMinTermFreq(1);
	filter.setUseStoplist(false);
	filter.setTFTransform(false);
	filter.setIDFTransform(false);		
	filter.setWordsToKeep(1000000000);
	NGramTokenizer tokenizer = new NGramTokenizer();
	tokenizer.setNGramMinSize(2);
	tokenizer.setNGramMaxSize(2);
	filter.setTokenizer(tokenizer);	
	Instances newData = weka.filters.Filter.useFilter(data, filter);
	return newData;
}
 
Example #15
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 #16
Source File: AbstractOutput.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Prints the classifications to the buffer.
 * 
 * @param classifier the classifier to use for printing the classifications
 * @param testset the data source to obtain the test instances from
 * @throws Exception if check fails or error occurs during printing of
 *           classifications
 */
public void printClassifications(Classifier classifier, DataSource testset)
    throws Exception {
  int i;
  Instances test;
  Instance inst;

  i = 0;
  testset.reset();

  if (classifier instanceof BatchPredictor) {
    test = testset.getDataSet(m_Header.classIndex());
    double[][] predictions = ((BatchPredictor) classifier)
        .distributionsForInstances(test);
    for (i = 0; i < test.numInstances(); i++) {
      printClassification(predictions[i], test.instance(i), i);
    }
  } else {
    test = testset.getStructure(m_Header.classIndex());
    while (testset.hasMoreElements(test)) {
      inst = testset.nextElement(test);
      doPrintClassification(classifier, inst, i);
      i++;
    }
  }
}
 
Example #17
Source File: Trainer.java    From sentiment-analysis with Apache License 2.0 6 votes vote down vote up
/**Returns the Feature-based Representations.*/
private Instances getFeature(String fileFeature) throws Exception{
	DataSource ds = new DataSource(fileFeature);
	Instances data =  ds.getDataSet();
	data.setClassIndex(1);
	StringToWordVector filter = new StringToWordVector();
	filter.setInputFormat(data);
	filter.setLowerCaseTokens(true);
	filter.setMinTermFreq(1);
	filter.setUseStoplist(false);
	filter.setTFTransform(false);
	filter.setIDFTransform(false);		
	filter.setWordsToKeep(1000000000);
	NGramTokenizer tokenizer = new NGramTokenizer();
	tokenizer.setNGramMinSize(1);
	tokenizer.setNGramMaxSize(1);
	filter.setTokenizer(tokenizer);	
	Instances newData = weka.filters.Filter.useFilter(data, filter);
	return newData;
}
 
Example #18
Source File: LoadModel.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 6 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try{
        J48 mytree = (J48) weka.core.SerializationHelper.read("/Users/admin/Documents/NetBeansProjects/LoadModel/myDT.model");
        
        DataSource src1 = new DataSource("/Users/admin/Documents/NetBeansProjects/LoadModel/segment-test.arff");
        Instances tdt = src1.getDataSet();
        tdt.setClassIndex(tdt.numAttributes() - 1);
        
        System.out.println("ActualClass \t ActualValue \t PredictedValue \t PredictedClass");
        for (int i = 0; i < tdt.numInstances(); i++) {
            String act = tdt.instance(i).stringValue(tdt.instance(i).numAttributes() - 1);
            double actual = tdt.instance(i).classValue();
            Instance inst = tdt.instance(i);
            double predict = mytree.classifyInstance(inst);
            String pred = inst.toString(inst.numAttributes() - 1);
            System.out.println(act + " \t\t " + actual + " \t\t " + predict + " \t\t " + pred);
        }
    }
    catch(Exception e){
        System.out.println("Error!!!!\n" + e.getMessage());
    }
}
 
Example #19
Source File: Trainer.java    From sentiment-analysis with Apache License 2.0 6 votes vote down vote up
/**Returns the text-based Representations.*/
private Instances getText(String fileText) throws Exception{
	DataSource ds = new DataSource(fileText);
	Instances data =  ds.getDataSet();
	data.setClassIndex(1);
	StringToWordVector filter = new StringToWordVector();
	filter.setInputFormat(data);
	filter.setLowerCaseTokens(true);
	filter.setMinTermFreq(1);
	filter.setUseStoplist(false);
	filter.setTFTransform(false);
	filter.setIDFTransform(false);		
	filter.setWordsToKeep(1000000000);
	NGramTokenizer tokenizer = new NGramTokenizer();
	tokenizer.setNGramMinSize(2);
	tokenizer.setNGramMaxSize(2);
	filter.setTokenizer(tokenizer);	
	Instances newData = weka.filters.Filter.useFilter(data, filter);
	return newData;
}
 
Example #20
Source File: Datasets.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 6 votes vote down vote up
/** 
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try {
            DataSource src = new DataSource("/Users/admin/wekafiles/data/weather.numeric.arff");
            Instances dt= src.getDataSet();
            
            System.out.println(dt.toSummaryString());
            
            ArffSaver as = new ArffSaver();
            as.setInstances(dt);
            as.setFile(new File("weather.arff"));
            as.writeBatch();
        
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}
 
Example #21
Source File: ROC.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 != 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 #22
Source File: JustBuild.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 != 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("Build BR classifier");
  BR classifier = new BR();
  // further configuration of classifier
  classifier.buildClassifier(data);
}
 
Example #23
Source File: Evaluation.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * loadDataset - load a dataset, given command line options specifying an arff file.
 * @param	options	command line options, specifying dataset filename
 * @param	T		set to 'T' if we want to load a test file (default 't': load train or train-test file)
 * @return	the dataset
 */
public static Instances loadDataset(String options[], char T) throws Exception {

	Instances D = null;
	String filename = Utils.getOption(T, options);

	// Check for filename
	if (filename == null || filename.isEmpty())
		throw new Exception("[Error] You did not specify a dataset!");

	// Check for existence of file
	File file = new File(filename);
	if (!file.exists())
		throw new Exception("[Error] File does not exist: " + filename);
	if (file.isDirectory())
		throw new Exception("[Error] "+filename+ " points to a directory!");

	try {
		DataSource source = new DataSource(filename);
		D = source.getDataSet();
	} catch(Exception e) {
		e.printStackTrace();
		throw new Exception("[Error] Failed to load Instances from file '"+filename+"'.");
	}

	return D;
}
 
Example #24
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));
  }
}
 
Example #25
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 #26
Source File: MacroCurve.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 != 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 #27
Source File: PrecisionRecall.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 != 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 #28
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 #29
Source File: EvaluationTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public static Instances loadInstances(String fn) {
	try {
		Instances D = DataSource.read("src/test/resources/" + fn);
		MLUtils.prepareData(D);
		return D;
	} catch(Exception e) {
		System.err.println("");
		e.printStackTrace();
		System.exit(1);
	}
	return null;
}
 
Example #30
Source File: AnchorpointsCreationTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void anchorpointsAreCreatedAndHaveTheValues() throws IOException, InvalidAnchorPointsException, AlgorithmException, InterruptedException, ClassNotFoundException, DatasetCreationException {
	int[] xValues = new int[] { 2, 4, 8, 16, 32, 64 };
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	try {
		DataSetDescription description = client.dataGet(42);
		File file = client.datasetGet(description);
		DataSource source = new DataSource(file.getCanonicalPath());
		dataset = source.getDataSet();
		dataset.setClassIndex(dataset.numAttributes() - 1);
		Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
		dataset.setClassIndex(targetAttribute.index());
	} catch (Exception e) {
		throw new IOException("Could not load data set from OpenML!", e);
	}

	// final LearningCurveExtrapolationMethod extrapolationMethod, final ISupervisedLearner<I, D> learner, final D dataset, final double trainsplit, final int[] anchorPoints,
	// final ISamplingAlgorithmFactory<?, D, ? extends ASamplingAlgorithm<D>> samplingAlgorithmFactory, final long seed

	WekaInstances simpleDataset = new WekaInstances(dataset);
	LearningCurveExtrapolator extrapolator = new LearningCurveExtrapolator((x, y, ds) -> {
		Assert.assertArrayEquals(x, xValues);
		for (int i = 0; i < y.length; i++) {
			Assert.assertTrue(y[i] > 0.0d);
		}
		return null;
	}, new WekaClassifier(new J48()), simpleDataset, 0.7d, xValues, new SystematicSamplingFactory<>(), 1l);
	extrapolator.extrapolateLearningCurve();
}