Example usage for weka.core.converters ArffLoader getDataSet

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

Introduction

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

Prototype

@Override
public Instances getDataSet() throws IOException 

Source Link

Document

Return the full data set.

Usage

From source file:ap.mavenproject1.HelloWeka.java

public static void main(String args[]) {
    Instances data = null;//from   w w  w.  ja va2 s.co  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);/*from  w  w w .  ja  v  a  2 s  .co  m*/
    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);
    }

    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);//from   w  w  w.  j  av  a2 s  .  c  o  m
    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);
    }

    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;/*from w ww  . j av a  2s .co 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:cn.ict.zyq.bestConf.util.DataIOFile.java

License:Open Source License

/**
 * Return the data set loaded from the Arff file at @param path
 *///  w  w  w .j a  va 2 s .c  o  m
public static Instances loadDataFromArffFile(String path) throws IOException {
    ArffLoader loader = new ArffLoader();
    loader.setSource(new File(path));
    Instances data = loader.getDataSet();

    System.out.println("\nHeader of dataset:\n");
    System.out.println(new Instances(data, 0));
    return data;
}

From source file:com.ctrl.DataSource.java

License:Open Source License

/**
 * Reads an ARFF file from a file.//from   w  ww .j  av  a  2s.c  o m
 * 
 * @param filename   the ARFF file to read
 * @return      the data
 * @throws Exception  if reading fails
 */
public static Instances read(String filename) throws Exception {
    ArffLoader loader = new ArffLoader();
    loader.setSource(new File(filename));
    Instances data = loader.getDataSet();
    return data;
}

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

License:Open Source License

@PostConstruct
private void init() {
    try {/*w w w  .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:core.DatabaseSaverEx.java

License:Open Source License

/** 
 * Sets the options. <p/>/* ww w.j  ava 2 s.  c o  m*/
 *
 <!-- options-start -->
 * Valid options are: <p/>
 * 
 * <pre> -url &lt;JDBC URL&gt;
 *  The JDBC URL to connect to.
 *  (default: from DatabaseUtils.props file)</pre>
 * 
 * <pre> -user &lt;name&gt;
 *  The user to connect with to the database.
 *  (default: none)</pre>
 * 
 * <pre> -password &lt;password&gt;
 *  The password to connect with to the database.
 *  (default: none)</pre>
 * 
 * <pre> -T &lt;table name&gt;
 *  The name of the table.
 *  (default: the relation name)</pre>
 * 
 * <pre> -P
 *  Add an ID column as primary key. The name is specified
 *  in the DatabaseUtils file ('idColumn'). The DatabaseLoader
 *  won't load this column.</pre>
 * 
 * <pre> -i &lt;input file name&gt;
 *  Input file in arff format that should be saved in database.</pre>
 * 
 <!-- options-end -->
 *
 * @param options the options
 * @throws Exception if options cannot be set
 */
public void setOptions(String[] options) throws Exception {

    String tableString, inputString, tmpStr;

    resetOptions();

    tmpStr = Utils.getOption("url", options);
    if (tmpStr.length() != 0)
        setUrl(tmpStr);

    tmpStr = Utils.getOption("user", options);
    if (tmpStr.length() != 0)
        setUser(tmpStr);

    tmpStr = Utils.getOption("password", options);
    if (tmpStr.length() != 0)
        setPassword(tmpStr);

    tableString = Utils.getOption('T', options);

    inputString = Utils.getOption('i', options);

    if (tableString.length() != 0) {
        m_tableName = tableString;
        m_tabName = false;
    }

    m_id = Utils.getFlag('P', options);

    if (inputString.length() != 0) {
        try {
            m_inputFile = inputString;
            ArffLoader al = new ArffLoader();
            File inputFile = new File(inputString);
            al.setSource(inputFile);
            setInstances(al.getDataSet());
            //System.out.println(getInstances());
            if (tableString.length() == 0)
                m_tableName = getInstances().relationName();
        } catch (Exception ex) {
            printException(ex);
            ex.printStackTrace();
        }
    }
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile1(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial1.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES//from  w  w  w. java  2  s  .c  o m
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[1] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(2);
        for (int j = 0; j < 2; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 1)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(1);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_add_autosentiment.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(1);
    test.setClassIndex(1);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}