Example usage for weka.core.converters ArffLoader setFile

List of usage examples for weka.core.converters ArffLoader setFile

Introduction

In this page you can find the example usage for weka.core.converters ArffLoader setFile.

Prototype

@Override
public void setFile(File file) throws IOException 

Source Link

Document

sets the source File

Usage

From source file:ap.mavenproject1.HelloWeka.java

public static void main(String args[]) {
    Instances data = null;//  w  w  w . ja  v a2  s . c  o m
    ArffLoader loader = new ArffLoader();
    try {

        loader.setFile(new File("C:\\Users\\USER\\Desktop\\data.arff"));

        data = loader.getDataSet();
        data.setClassIndex(data.numAttributes() - 1);

    } catch (IOException ex) {
        Logger.getLogger(HelloWeka.class.getName()).log(Level.SEVERE, null, ex);
    }

    Apriori apriori = new Apriori();
    try {
        NumericToNominal numericToNominal = new NumericToNominal();
        numericToNominal.setInputFormat(data);

        Instances nominalData = Filter.useFilter(data, numericToNominal);
        apriori.buildAssociations(nominalData);
        FastVector[] allTheRules;
        allTheRules = apriori.getAllTheRules();
        for (int i = 0; i < allTheRules.length; i++) {
            System.out.println(allTheRules[i]);
        }
        //             BufferedWriter writer = new BufferedWriter(new FileWriter("./output.arff"));
        //                writer.write(nominalData.toString());
        //                writer.flush();
        //                writer.close();

    } catch (Exception ex) {
        Logger.getLogger(HelloWeka.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:boostingPL.boosting.AdaBoost.java

License:Open Source License

public static void main(String[] args) throws Exception {
    java.io.File inputFile = new java.io.File(
            "/home/aax/xpShareSpace/dataset/single-class/+winered/winequality-red.datatrain1.arff");
    ArffLoader atf = new ArffLoader();
    atf.setFile(inputFile);
    Instances training = atf.getDataSet();
    training.setClassIndex(training.numAttributes() - 1);

    AdaBoost adaBoost = new AdaBoost(training, 100);
    for (int t = 0; t < 100; t++) {
        adaBoost.run(t);/*from   w  w  w. ja  v  a2  s  .c om*/
    }

    java.io.File inputFilet = new java.io.File(
            "/home/aax/xpShareSpace/dataset/single-class/+winered/winequality-red.datatest1.arff");
    ArffLoader atft = new ArffLoader();
    atft.setFile(inputFilet);
    Instances testing = atft.getDataSet();
    testing.setClassIndex(testing.numAttributes() - 1);

    Evaluation eval = new Evaluation(testing);
    for (Instance inst : testing) {
        eval.evaluateModelOnceAndRecordPrediction(adaBoost, inst);
    }
    System.out.println(eval.toSummaryString());
    System.out.println(eval.toClassDetailsString());
    System.out.println(eval.toMatrixString());

    /*
    int right = 0;
    for (int i = 0; i < testing.numInstances(); i++) {
       Instance inst = testing.instance(i);
       if (adaBoost.classifyInstance(inst) == inst.classValue()) {
    right++;
       }
    }
    System.out.println(right);
    System.out.println((double)right/training.numInstances());
    */
}

From source file:boostingPL.boosting.SAMME.java

License:Open Source License

public static void main(String[] args) throws Exception {
    java.io.File inputFile = new java.io.File(args[0]);
    ArffLoader atf = new ArffLoader();
    atf.setFile(inputFile);
    Instances training = atf.getDataSet();
    training.setClassIndex(training.numAttributes() - 1);
    //Instances testing = new Instances(training);

    int iterationNum = 100;
    SAMME samme = new SAMME(training, iterationNum);
    for (int t = 0; t < iterationNum; t++) {
        samme.run(t);/* www.  j a v  a  2  s. co m*/
    }

    java.io.File inputFilet = new java.io.File(args[1]);
    ArffLoader atft = new ArffLoader();
    atft.setFile(inputFilet);
    Instances testing = atft.getDataSet();
    testing.setClassIndex(testing.numAttributes() - 1);

    Evaluation eval = new Evaluation(testing);
    for (Instance inst : testing) {
        eval.evaluateModelOnceAndRecordPrediction(samme, inst);
    }
    System.out.println(eval.toSummaryString());
    System.out.println(eval.toClassDetailsString());
    System.out.println(eval.toMatrixString());
}

From source file:br.com.edu.partition.Tranning.java

public static Double Tranning_JRIP(String test, String tranning) throws IOException, Exception {
    Double result_ = null;// w  w w .  j  a  v a  2 s  . c  o m
    ArffLoader loader;
    loader = new ArffLoader();
    loader.setFile(new File(tranning));
    loader.getStructure();

    Instances trainingset = loader.getDataSet();
    int classIndex = trainingset.numAttributes() - 1;
    trainingset.setClassIndex(classIndex);

    //J48 j48 = new J48();
    JRip jRip = new JRip();
    //String[] options2 = {"-F", "3", "-N", "2.0", "-O", "2", "-S", "1"};
    //jRip.setOptions(options2);
    //jRip.buildClassifier(trainingset);
    jRip.buildClassifier(trainingset);

    loader = new ArffLoader();
    loader.setFile(new File(test));
    loader.getStructure();

    Instances testset = loader.getDataSet();
    testset.setClassIndex(testset.numAttributes() - 1);
    for (Instance instance : testset) {
        //double[] result = jRip.distributionForInstance(instance);
        double[] result = jRip.distributionForInstance(instance);
        result_ = result[1];
        //System.out.println(test + " " + result[1] + " " + tranning);
    }
    return result_;

}

From source file:c4.pkg5crossv.DataLoad.java

public static Instances loadData(String fileName) throws IOException {
    ArffLoader loader = new ArffLoader(); //Utworzenie obiektu czytajacego dane z formatu ARFF
    loader.setFile(new File(fileName)); //Ustawienie pliku do odczytania
    return loader.getDataSet(); //Odczytanie danych z pliku
}

From source file:CEP.GenerateStream.java

public void MakeStream() {
    File file = new File("C:\\Users\\Weary\\Documents\\w4ndata\\w4ndata.arff");
    String pc = System.getProperty("user.dir").toString();
    if (pc.contains("gs023850")) {
        file = new File("C:\\Users\\gs023850\\Documents\\w4ndata\\w4ndata.arff");
    }//from   www.  j  a  v a  2s .  c o  m
    try {
        ArffLoader loader = new ArffLoader();
        loader.setFile(file);
        Instances structure = loader.getStructure();
        int j = structure.numAttributes();

        HeaderManager.SetStructure(new Instances(structure));
        Instance current;
        long previousTimeStamp = 0;
        String timeStamp = "0";
        long wait = 0;

        while ((current = loader.getNextInstance(structure)) != null) {
            timeStamp = current.stringValue(0);
            cepRT.sendEvent(current);
            System.out.println("Sending event");
            previousTimeStamp = WaitTime(timeStamp, previousTimeStamp, wait);
        }
    } catch (Exception e) {
        if (e.equals(new FileNotFoundException())) {
            System.out.println("File not found - could not generate stream");
            return;
        } else if (e.equals(new IOException())) {
            System.out.println("Unable to read file");
        } else if (e.equals(new NumberFormatException())) {
            System.out.println("Unable to convert to time to number - bad time");
        } else {
            System.out.println(e.toString());
        }
    }
}

From source file:com.daniel.convert.IncrementalClassifier.java

License:Open Source License

/**
 * Expects an ARFF file as first argument (class attribute is assumed to be
 * the last attribute).//from w w w  .  ja va2 s .  c  o m
 * 
 * @param args
 *            the commandline arguments
 * @throws Exception
 *             if something goes wrong
 */
public static BayesNet treinar(String[] args) throws Exception {
    // load data
    ArffLoader loader = new ArffLoader();
    loader.setFile(new File(args[0]));
    Instances structure = loader.getStructure();
    structure.setClassIndex(structure.numAttributes() - 1);

    // train NaiveBayes
    BayesNet BayesNet = new BayesNet();

    Instance current;
    while ((current = loader.getNextInstance(structure)) != null) {
        structure.add(current);
    }
    BayesNet.buildClassifier(structure);

    // output generated model
    // System.out.println(nb);

    // test set
    BayesNet BayesNetTest = new BayesNet();

    // test the model
    Evaluation eTest = new Evaluation(structure);
    // eTest.evaluateModel(nb, structure);
    eTest.crossValidateModel(BayesNetTest, structure, 15, new Random(1));

    // Print the result  la Weka explorer:
    String strSummary = eTest.toSummaryString();
    System.out.println(strSummary);

    return BayesNet;
}

From source file:com.reactivetechnologies.analytics.core.eval.CombinerDatasetGenerator.java

License:Open Source License

@PostConstruct
private void init() {
    try {/*www. j a v  a 2 s  .  c  o m*/
        ArffLoader loader = new ArffLoader();
        File f = ResourceUtils.getFile(dummyFile);
        loader.setFile(f);
        log.info("Reading file [" + f + "] for creating evaluation dataset");
        instances = loader.getDataSet();
    } catch (Exception e) {
        log.error("EvaluationDatasetGenerator::init [ " + e.getMessage() + "] Will go with a dummy dataset. ");
        log.debug("", e);
        try {
            instances = new RandomRBF().generateExamples();
        } catch (Exception e1) {
            log.debug("", e);
        }
    }
    instances.setClassIndex(instances.numAttributes() - 1);
}

From source file:cs.man.ac.uk.predict.Predictor.java

License:Open Source License

public static void makePredictionsEnsembleNew(String trainPath, String testPath, String resultPath) {
    System.out.println("Training set: " + trainPath);
    System.out.println("Test set: " + testPath);

    /**/*  www  .  j  a va  2s.  c  o  m*/
     * The ensemble classifiers. This is a heterogeneous ensemble.
     */
    J48 learner1 = new J48();
    SMO learner2 = new SMO();
    NaiveBayes learner3 = new NaiveBayes();
    MultilayerPerceptron learner5 = new MultilayerPerceptron();

    System.out.println("Training Ensemble.");
    long startTime = System.nanoTime();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(trainPath));
        Instances data = new Instances(reader);
        data.setClassIndex(data.numAttributes() - 1);
        System.out.println("Training data length: " + data.numInstances());

        learner1.buildClassifier(data);
        learner2.buildClassifier(data);
        learner3.buildClassifier(data);
        learner5.buildClassifier(data);

        long endTime = System.nanoTime();
        long nanoseconds = endTime - startTime;
        double seconds = (double) nanoseconds / 1000000000.0;
        System.out.println("Training Ensemble completed in " + nanoseconds + " (ns) or " + seconds + " (s).");
    } catch (IOException e) {
        System.out.println("Could not train Ensemble classifier IOException on training data file.");
    } catch (Exception e) {
        System.out.println("Could not train Ensemble classifier Exception building model.");
    }

    try {
        String line = "";

        // Read the file and display it line by line. 
        BufferedReader in = null;

        // Read in and store each positive prediction in the tree map.
        try {
            //open stream to file
            in = new BufferedReader(new FileReader(testPath));

            while ((line = in.readLine()) != null) {
                if (line.toLowerCase().contains("@data"))
                    break;
            }
        } catch (Exception e) {
        }

        // A different ARFF loader used here (compared to above) as
        // the ARFF file may be extremely large. In which case the whole
        // file cannot be read in. Instead it is read in incrementally.
        ArffLoader loader = new ArffLoader();
        loader.setFile(new File(testPath));

        Instances data = loader.getStructure();
        data.setClassIndex(data.numAttributes() - 1);

        System.out.println("Ensemble Classifier is ready.");
        System.out.println("Testing on all instances avaialable.");

        startTime = System.nanoTime();

        int instanceNumber = 0;

        // label instances
        Instance current;

        while ((current = loader.getNextInstance(data)) != null) {
            instanceNumber += 1;
            line = in.readLine();

            double classification1 = learner1.classifyInstance(current);
            double classification2 = learner2.classifyInstance(current);
            double classification3 = learner3.classifyInstance(current);
            double classification5 = learner5.classifyInstance(current);

            // All classifiers must agree. This is a very primitive ensemble strategy!
            if (classification1 == 1 && classification2 == 1 && classification3 == 1 && classification5 == 1) {
                if (line != null) {
                    //System.out.println("Instance: "+instanceNumber+"\t"+line);
                    //System.in.read();
                }
                Writer.append(resultPath, instanceNumber + "\n");
            }
        }

        in.close();

        System.out.println("Test set instances: " + instanceNumber);

        long endTime = System.nanoTime();
        long duration = endTime - startTime;
        double seconds = (double) duration / 1000000000.0;

        System.out.println("Testing Ensemble completed in " + duration + " (ns) or " + seconds + " (s).");
    } catch (Exception e) {
        System.out.println("Could not test Ensemble classifier due to an error.");
    }
}

From source file:cs.man.ac.uk.predict.Predictor.java

License:Open Source License

public static void makePredictionsEnsembleStream(String trainPath, String testPath, String resultPath) {
    System.out.println("Training set: " + trainPath);
    System.out.println("Test set: " + testPath);

    /**// ww  w. ja v  a  2s  .c o m
     * The ensemble classifiers. This is a heterogeneous ensemble.
     */
    J48 learner1 = new J48();
    SMO learner2 = new SMO();
    NaiveBayes learner3 = new NaiveBayes();
    MultilayerPerceptron learner5 = new MultilayerPerceptron();

    System.out.println("Training Ensemble.");
    long startTime = System.nanoTime();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(trainPath));
        Instances data = new Instances(reader);
        data.setClassIndex(data.numAttributes() - 1);
        System.out.println("Training data length: " + data.numInstances());

        learner1.buildClassifier(data);
        learner2.buildClassifier(data);
        learner3.buildClassifier(data);
        learner5.buildClassifier(data);

        long endTime = System.nanoTime();
        long nanoseconds = endTime - startTime;
        double seconds = (double) nanoseconds / 1000000000.0;
        System.out.println("Training Ensemble completed in " + nanoseconds + " (ns) or " + seconds + " (s).");
    } catch (IOException e) {
        System.out.println("Could not train Ensemble classifier IOException on training data file.");
    } catch (Exception e) {
        System.out.println("Could not train Ensemble classifier Exception building model.");
    }

    try {
        // A different ARFF loader used here (compared to above) as
        // the ARFF file may be extremely large. In which case the whole
        // file cannot be read in. Instead it is read in incrementally.
        ArffLoader loader = new ArffLoader();
        loader.setFile(new File(testPath));

        Instances data = loader.getStructure();
        data.setClassIndex(data.numAttributes() - 1);

        System.out.println("Ensemble Classifier is ready.");
        System.out.println("Testing on all instances avaialable.");

        startTime = System.nanoTime();

        int instanceNumber = 0;

        // label instances
        Instance current;

        while ((current = loader.getNextInstance(data)) != null) {
            instanceNumber += 1;

            double classification1 = learner1.classifyInstance(current);
            double classification2 = learner2.classifyInstance(current);
            double classification3 = learner3.classifyInstance(current);
            double classification5 = learner5.classifyInstance(current);

            // All classifiers must agree. This is a very primitive ensemble strategy!
            if (classification1 == 1 && classification2 == 1 && classification3 == 1 && classification5 == 1) {
                Writer.append(resultPath, instanceNumber + "\n");
            }
        }

        System.out.println("Test set instances: " + instanceNumber);

        long endTime = System.nanoTime();
        long duration = endTime - startTime;
        double seconds = (double) duration / 1000000000.0;

        System.out.println("Testing Ensemble completed in " + duration + " (ns) or " + seconds + " (s).");
    } catch (Exception e) {
        System.out.println("Could not test Ensemble classifier due to an error.");
    }
}