Java Code Examples for weka.core.converters.ConverterUtils.DataSource

The following examples show how to use weka.core.converters.ConverterUtils.DataSource. 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 check out the related API usage on the sidebar.
Example 1
Source Project: tsml   Source File: AbstractOutput.java    License: 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 2
/**
 * @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 3
/**
 * @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 4
/** 
 * @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 5
/**
 * @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 6
/**
 * @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 7
/**
 * @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 8
/**
 * @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 9
Source Project: Java-Data-Analysis   Source File: TestWekaBayes.java    License: MIT License 6 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 10
@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 11
@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 12
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 13
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 14
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 15
Source Project: meka   Source File: MicroCurve.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);

  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: PrepareClassAttributes.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 != 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 17
Source Project: meka   Source File: MekaSearch.java    License: 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 18
Source Project: sentiment-analysis   Source File: Trainer.java    License: 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 19
Source Project: sentiment-analysis   Source File: Trainer.java    License: 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 20
Source Project: sentiment-analysis   Source File: Trainer.java    License: 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 21
Source Project: NeurophFramework   Source File: WekaNeurophSample.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // create weka dataset from file
        DataSource dataSource = new DataSource("datasets/iris.arff");
        Instances wekaDataset = dataSource.getDataSet();
        wekaDataset.setClassIndex(4);

        // normalize dataset
        Normalize filter = new Normalize();
        filter.setInputFormat(wekaDataset);
        wekaDataset = Filter.useFilter(wekaDataset, filter);    
        
        // convert weka dataset to neuroph dataset
        DataSet neurophDataset = WekaDataSetConverter.convertWekaToNeurophDataset(wekaDataset, 4, 3);

        // convert back neuroph dataset to weka dataset
        Instances testWekaDataset = WekaDataSetConverter.convertNeurophToWekaDataset(neurophDataset);

        // print out all to compare
        System.out.println("Weka data set from file");
        printDataSet(wekaDataset);
        
        System.out.println("Neuroph data set converted from Weka data set");
        printDataSet(neurophDataset);
        
        System.out.println("Weka data set reconverted from Neuroph data set");
        printDataSet(testWekaDataset);

        System.out.println("Testing WekaNeurophClassifier");
        testNeurophWekaClassifier(wekaDataset);
    }
 
Example 22
Source Project: tsml   Source File: AttributeSelection.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Perform attribute selection with a particular evaluator and
 * a set of options specifying search method and input file etc.
 *
 * @param ASEvaluator an evaluator object
 * @param options an array of options, not only for the evaluator
 * but also the search method (if any) and an input data file
 * @return the results of attribute selection as a String
 * @exception Exception if no training file is set
 */
public static String SelectAttributes (ASEvaluation ASEvaluator, 
			 String[] options)
  throws Exception {
  String trainFileName, searchName;
  Instances train = null;
  ASSearch searchMethod = null;
  String[] optionsTmp = (String[]) options.clone();
  boolean helpRequested = false;

  try {
    // get basic options (options the same for all attribute selectors
    trainFileName = Utils.getOption('i', options);
    helpRequested = Utils.getFlag('h', optionsTmp);

    if (helpRequested || (trainFileName.length() == 0)) {
      searchName = Utils.getOption('s', optionsTmp);
      if (searchName.length() != 0) {
        String[] searchOptions = Utils.splitOptions(searchName);
        searchMethod = (ASSearch)Class.forName(searchOptions[0]).newInstance();
      }

      if (helpRequested)
        throw new Exception("Help requested.");
      else
        throw new Exception("No training file given.");
    }
  }
  catch (Exception e) {
    throw  new Exception('\n' + e.getMessage() 
	   + makeOptionString(ASEvaluator, searchMethod));
  }

  DataSource source = new DataSource(trainFileName);
  train = source.getDataSet();
  return SelectAttributes(ASEvaluator, options, train);
}
 
Example 23
/**
 * @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/MakingPredictions/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);

        DataSource src1 = new DataSource("/Users/admin/Documents/NetBeansProjects/MakingPredictions/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(e.getCause());
    }
}
 
Example 24
/**
 * @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/DevelopClassifier/vote.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 tree = new J48();
        tree.setOptions(options);
        tree.buildClassifier(dt);
        System.out.println(tree.getCapabilities().toString());
        System.out.println(tree.graph());
        
        //uncomment the following three lines of code for Naive Bayes 
        NaiveBayes nb = new NaiveBayes();
        nb.buildClassifier(dt);
        System.out.println(nb.getCapabilities().toString());
        
        }        
    catch(Exception e){
        System.out.println("Error!!!!\n" + e.getMessage());
    }
}
 
Example 25
Source Project: MonitorClient   Source File: NavieBayesClassifier.java    License: Apache License 2.0 5 votes vote down vote up
public void loadArff(String path){
    try {
        DataSource source = new DataSource(path);
        instances=source.getDataSet();
        if (instances.classIndex() == -1)
            instances.setClassIndex(instances.numAttributes() - 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 26
Source Project: Java-Data-Analysis   Source File: TestDataSource.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    DataSource source = new DataSource("data/fruit.arff");
    
    Instances instances = source.getDataSet();
    instances.setClassIndex(instances.numAttributes() - 1);
    System.out.println(instances.attribute(2));
    
    Instance instance = instances.get(3);
    System.out.println(instance);
    System.out.println(instance.stringValue(0));
    System.out.println(instance.stringValue(2));
}
 
Example 27
Source Project: Java-Data-Analysis   Source File: TestWekaJ48.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    DataSource source = new DataSource("data/AnonFruit.arff");
    Instances instances = source.getDataSet();
    instances.setClassIndex(3);  // target attribute: (Sweet)
    
    J48 j48 = new J48();  // an extension of ID3
    j48.setOptions(new String[]{"-U"});  // use unpruned tree
    j48.buildClassifier(instances);

    for (Instance instance : instances) {
        double prediction = j48.classifyInstance(instance);
        System.out.printf("%4.0f%4.0f%n", 
                instance.classValue(), prediction);
    }
}
 
Example 28
Source Project: Java-Data-Analysis   Source File: TestIBk.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    DataSource source = new DataSource("data/AnonFruit.arff");
    Instances instances = source.getDataSet();
    instances.setClassIndex(3);  // target attribute: (Sweet)
    
    IBk ibk = new IBk();
    ibk.buildClassifier(instances);

    for (Instance instance : instances) {
        double prediction = ibk.classifyInstance(instance);
        System.out.printf("%4.0f%4.0f%n", 
                instance.classValue(), prediction);
    }
}
 
Example 29
private Instances loadDataset(final String path) {
	Instances dataset = null;
	try {
		dataset = DataSource.read(path);
		if (dataset.classIndex() == -1) {
			dataset.setClassIndex(dataset.numAttributes() - 1);
		}
	} catch (Exception e) {
		this.logger.error(e.getMessage());
	}

	return dataset;
}
 
Example 30
Source Project: AILibs   Source File: FileUtils.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public static List<Instances> readInstances(final String dirPath, final String dataSetPrefix, final String excludePostfix) {
	List<Instances> results = new ArrayList<>();

	File dir = new File(dirPath);
	File[] dataSetFiles = dir.listFiles((dir1, name) -> {
		if (excludePostfix != null) {
			return name.startsWith(dataSetPrefix) && !name.endsWith(excludePostfix);
		} else {
			return name.startsWith(dataSetPrefix);
		}
	});
	for (File file : dataSetFiles) {
		try {
			DataSource source = new DataSource(new FileInputStream(file));
			Instances insts = source.getDataSet();
			insts.setClassIndex(insts.numAttributes() - 1);

			results.add(insts);

		} catch (Exception e) {
			logger.error("Could not import data set. Reason: {}", e.getMessage());
			return new ArrayList<>();
		}
	}

	return results;
}