Example usage for weka.filters.supervised.instance Resample setOptions

List of usage examples for weka.filters.supervised.instance Resample setOptions

Introduction

In this page you can find the example usage for weka.filters.supervised.instance Resample setOptions.

Prototype

@Override
public void setOptions(String[] options) throws Exception 

Source Link

Document

Parses a given list of options.

Usage

From source file:id3j48.WekaAccess.java

public static Instances resampleData(Instances data) throws Exception {
    Resample resample = new Resample();
    String filterOptions = "-B 0.0 -S 1 -Z 100.0";
    resample.setOptions(Utils.splitOptions(filterOptions));
    resample.setRandomSeed(1);// www .  j  av a2 s.  c  o  m
    resample.setInputFormat(data);
    Instances newDataSet = Filter.useFilter(data, resample);
    return newDataSet;
}

From source file:org.openml.webapplication.generatefolds.GenerateFolds.java

License:Open Source License

private Instances sample_splits_bootstrap(String name) throws Exception {
    Instances splits = new Instances(name, am.getArffHeader(), splits_size);
    for (int r = 0; r < evaluationMethod.getRepeats(); ++r) {
        Resample resample = new Resample();
        String[] resampleOptions = { "-B", "0.0", "-Z", "100.0", "-S", r + "" };
        resample.setOptions(resampleOptions);
        resample.setInputFormat(dataset);
        Instances trainingsset = Filter.useFilter(dataset, resample);

        // create training set, consisting of instances from 
        for (int i = 0; i < trainingsset.numInstances(); ++i) {
            int rowid = (int) trainingsset.instance(i).value(0);
            splits.add(am.createInstance(true, rowid, r, 0));
        }//from w w  w.j a v  a2  s.  com
        for (int i = 0; i < dataset.numInstances(); ++i) {
            int rowid = (int) dataset.instance(i).value(0);
            splits.add(am.createInstance(false, rowid, r, 0));
        }
    }
    return splits;
}

From source file:recsys.BuildModel.java

public static void main(String args[]) throws Exception {

    //Opening the training file
    int own_training = StaticVariables.own_training;
    DataSource sourceTrain;// w  ww .ja v  a2  s  . c o m
    if (own_training == 1)
        sourceTrain = new DataSource("D://own_training//item//feature data//train_feature.arff");
    else
        sourceTrain = new DataSource("E://recsys//item//feature data//train_feature.arff");

    Instances train = sourceTrain.getDataSet();

    String[] options = new String[2];
    options[0] = "-R"; // "range"
    options[1] = "1,2,4"; // first attribute
    //options[2] = "2";                                     // first attribute
    //options[3] = "4";         
    //options[2] = "9";                                     // first attribute
    //options[3] = "3";                                     // first attribute
    //options[4] = "4";                                     // first attribute

    Remove remove = new Remove(); // new instance of filter
    remove.setOptions(options); // set options
    remove.setInputFormat(train); // inform filter about dataset **AFTER** setting options
    Instances newData = Filter.useFilter(train, remove); // apply filter
    System.out.println("number of attributes " + newData.numAttributes());

    System.out.println(newData.firstInstance());

    if (newData.classIndex() == -1) {
        newData.setClassIndex(newData.numAttributes() - 1);
    }

    Resample sampler = new Resample();
    String Fliteroptions = "-B 1.0";
    sampler.setOptions(weka.core.Utils.splitOptions(Fliteroptions));
    sampler.setRandomSeed((int) System.currentTimeMillis());
    sampler.setInputFormat(newData);
    newData = Resample.useFilter(newData, sampler);

    //Normalize normalize = new Normalize();
    //normalize.toSource(Fliteroptions, newData);
    //Remove remove = new Remove();                         // new instance of filter
    //remove.setOptions(options);                           // set options
    //remove.setInputFormat(train);                          // inform filter about dataset **AFTER** setting options
    //Instances newData = Filter.useFilter(train, remove);   // apply filter

    //rm.setAttributeIndices("2");
    //rm.setAttributeIndices("3");
    //rm.setAttributeIndices("4");
    //rm.setAttributeIndices("5");
    //rm.setAttributeIndices("6");

    //rm.setAttributeIndices("6");
    //rm.setAttributeIndices("5");

    //Remove rm = new Remove();
    //rm.setInputFormat(train);
    //rm.setAttributeIndices("1");
    //FilteredClassifier fc = new FilteredClassifier();
    //cls.setOptions(args);
    //J48 cls = new J48();
    //LibSVM cls = new LibSVM();
    //SMO cls = new SMO();
    //Logistic cls = new Logistic();
    //BayesianLogisticRegression cls = new BayesianLogisticRegression();
    //cls.setThreshold(0.52);
    //AdaBoostM1 cls = new AdaBoostM1();
    //NaiveBayes cls = new NaiveBayes();
    //weka.classifiers.meta.Bagging cls = new Bagging();
    //weka.classifiers.functions.IsotonicRegression cls = new IsotonicRegression();
    //j48.setUnpruned(true);        // using an unpruned J48
    // meta-classifier

    //BayesNet cls = new BayesNet();
    RandomForest cls = new RandomForest();
    //cls.setNumTrees(100);
    //cls.setMaxDepth(3);
    //cls.setNumFeatures(3);

    //fc.setClassifier(cls);
    //fc.setFilter(rm);

    // train and make predictions
    //System.out.println(fc.globalInfo());
    //System.out.println(fc.getFilter());
    //fc.buildClassifier(train);
    cls.buildClassifier(newData);
    //Evaluation eval = new Evaluation(newData);
    //Random rand = new Random(1);  // using seed = 1
    //int folds = 2;
    //eval.crossValidateModel(cls, newData, folds, rand);
    //System.out.println(eval.toSummaryString());
    //System.out.println("precision on buy " + eval.precision(newData.classAttribute().indexOfValue("buy")));

    //System.out.println("recall on buy " + eval.recall(newData.classAttribute().indexOfValue("buy")));
    //System.out.println(eval.confusionMatrix().toString());
    //System.out.println("Precision " + eval.precision(newData.classIndex()-1));
    //System.out.println("Recall " + eval.recall(newData.classIndex()-1));
    //Classfier cls = new weka.classifiers.bayes.NaiveBayes();
    //FilteredClassifier fc = new FilteredClassifier();
    //fc.setFilter(rm);
    //fc.setClassifier(cls);

    //train and make predictions
    //fc.buildClassifier(train);

    // serialize model
    ObjectOutputStream oos;
    if (own_training == 1)
        oos = new ObjectOutputStream(new FileOutputStream("D://own_training//item//model//train.model"));
    else
        oos = new ObjectOutputStream(new FileOutputStream("E://recsys//item//model//train.model"));

    oos.writeObject(cls);
    oos.flush();
    oos.close();
}