Example usage for weka.classifiers.timeseries WekaForecaster primeForecaster

List of usage examples for weka.classifiers.timeseries WekaForecaster primeForecaster

Introduction

In this page you can find the example usage for weka.classifiers.timeseries WekaForecaster primeForecaster.

Prototype

@Override
public void primeForecaster(Instances insts) throws Exception 

Source Link

Document

Supply the (potentially) trained model with enough historical data, up to and including the current time point, in order to produce a forecast.

Usage

From source file:org.datahack.forecast.BayExample.java

public static void main(String[] args) {
    try {/* w  w w.  ja v a 2  s  .  co m*/
        // path to the Australian wine data included with the time series forecasting
        // package

        File output = File.createTempFile("tempWineData", "arff");
        output.deleteOnExit();
        String dataFileName = "sample-data/wine.arff";
        URL resource = BayExample.class.getResource("/" + dataFileName);
        FileUtils.copyURLToFile(resource, output);

        String pathToWineData = output.getPath();

        // load the wine data
        Instances wine = new Instances(new BufferedReader(new FileReader(pathToWineData)));

        InstanceQuery q = new InstanceQuery();

        // new forecaster
        WekaForecaster forecaster = new WekaForecaster();

        // set the targets we want to forecast. This method calls
        // setFieldsToLag() on the lag maker object for us
        forecaster.setFieldsToForecast("Fortified,Dry-white");

        // default underlying classifier is SMOreg (SVM) - we'll use
        // gaussian processes for regression instead
        forecaster.setBaseForecaster(new GaussianProcesses());

        forecaster.getTSLagMaker().setTimeStampField("Date"); // date time stamp
        forecaster.getTSLagMaker().setMinLag(1);
        forecaster.getTSLagMaker().setMaxLag(12); // monthly data

        // add a month of the year indicator field
        forecaster.getTSLagMaker().setAddMonthOfYear(true);

        // add a quarter of the year indicator field
        forecaster.getTSLagMaker().setAddQuarterOfYear(true);

        // build the model
        forecaster.buildForecaster(wine, System.out);

        // prime the forecaster with enough recent historical data
        // to cover up to the maximum lag. In our case, we could just supply
        // the 12 most recent historical instances, as this covers our maximum
        // lag period
        forecaster.primeForecaster(wine);

        // forecast for 12 units (months) beyond the end of the
        // training data
        List<List<NumericPrediction>> forecast = forecaster.forecast(12, System.out);

        // output the predictions. Outer list is over the steps; inner list is over
        // the targets
        for (int i = 0; i < 12; i++) {
            List<NumericPrediction> predsAtStep = forecast.get(i);
            for (int j = 0; j < 2; j++) {
                NumericPrediction predForTarget = predsAtStep.get(j);
                System.out.print("" + predForTarget.predicted() + " ");
            }
            System.out.println();
        }

        // we can continue to use the trained forecaster for further forecasting
        // by priming with the most recent historical data (as it becomes available).
        // At some stage it becomes prudent to re-build the model using current
        // historical data.

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.datahack.forecast.TimeSeriesExample.java

public static void main(String[] args) {
    try {/* ww  w  .ja  v a 2  s.  co m*/
        // path to the Australian wine data included with the time series forecasting
        // package

        File output = File.createTempFile("tempWineData", "arff");
        output.deleteOnExit();
        String dataFileName = "sample-data/parking344.arff";
        URL resource = TimeSeriesExample.class.getResource("/" + dataFileName);
        FileUtils.copyURLToFile(resource, output);

        String pathToWineData = output.getPath();

        // load the wine data
        Instances wine = new Instances(new BufferedReader(new FileReader(pathToWineData)));

        // new forecaster
        WekaForecaster forecaster = new WekaForecaster();

        // set the targets we want to forecast. This method calls
        // setFieldsToLag() on the lag maker object for us
        forecaster.setFieldsToForecast("occupiedSpaces");

        // default underlying classifier is SMOreg (SVM) - we'll use
        // gaussian processes for regression instead
        forecaster.setBaseForecaster(new GaussianProcesses());

        forecaster.getTSLagMaker().setTimeStampField("eventTime"); // date time stamp
        forecaster.getTSLagMaker().setMinLag(1);
        forecaster.getTSLagMaker().setMaxLag(12); // monthly data

        // add a month of the year indicator field
        forecaster.getTSLagMaker().setAddMonthOfYear(true);

        // add a quarter of the year indicator field
        forecaster.getTSLagMaker().setAddQuarterOfYear(true);

        // build the model
        forecaster.buildForecaster(wine, System.out);

        // prime the forecaster with enough recent historical data
        // to cover up to the maximum lag. In our case, we could just supply
        // the 12 most recent historical instances, as this covers our maximum
        // lag period
        forecaster.primeForecaster(wine);

        // forecast for 12 units (months) beyond the end of the
        // training data
        List<List<NumericPrediction>> forecast = forecaster.forecast(12, System.out);

        // output the predictions. Outer list is over the steps; inner list is over
        // the targets
        for (int i = 0; i < 12; i++) {
            List<NumericPrediction> predsAtStep = forecast.get(i);
            for (int j = 0; j < 2; j++) {
                NumericPrediction predForTarget = predsAtStep.get(j);
                System.out.print("" + predForTarget.predicted() + " ");
            }
            System.out.println();
        }

        // we can continue to use the trained forecaster for further forecasting
        // by priming with the most recent historical data (as it becomes available).
        // At some stage it becomes prudent to re-build the model using current
        // historical data.

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Prediccion.Prediccion.java

License:Open Source License

@Override
public void run() {
    try {/*from   w  ww. j  a v  a  2  s. c om*/

        ArrayList<Instances> pasos = cargarDatos();

        System.err.println(pasos.size());

        //Instanciamos el predictor
        ArrayList<WekaForecaster> forecaster = new ArrayList<>(24);

        for (int a = 0; a < 24; a++) {
            forecaster.add(new WekaForecaster());
        }

        int a = 0;

        for (WekaForecaster fore : forecaster) {

            //Defimimos el atributo que queremos predecir
            fore.setFieldsToForecast("Total");

            //Definimos el mtodo de prediccin a emplear. En este caso, regresin lineal porque 
            //en el artculo es el que mejor ha funcionado
            fore.setBaseForecaster(new LinearRegression());

            //Defimimos el atributo que "marca" el tiempo y su peridiocidad
            fore.getTSLagMaker().setTimeStampField("Intervalo");
            fore.getTSLagMaker().setMinLag(1);
            fore.getTSLagMaker().setMaxLag(1);

            fore.getTSLagMaker().setPeriodicity(TSLagMaker.Periodicity.WEEKLY);

            fore.buildForecaster(pasos.get(a), System.out);

            // System.err.println(pasos.get(a).toString());

            //System.err.printf("Termin");

            fore.primeForecaster(pasos.get(a));

            List<List<NumericPrediction>> forecast = fore.forecast(1, System.out);

            System.err.println("==== " + a + " ====");
            // output the predictions. Outer list is over the steps; inner list is over
            // the targets
            for (int i = 0; i < 1; i++) {
                List<NumericPrediction> predsAtStep = forecast.get(i);
                for (int j = 0; j < 1; j++) {
                    NumericPrediction predForTarget = predsAtStep.get(j);
                    System.err.print("" + predForTarget.predicted() + " ");
                }
                System.err.println();
            }
            a++;
        }
        /*    
                    
           // path to the Australian wine data included with the time series forecasting
           // package
           String pathToWineData = weka.core.WekaPackageManager.PACKAGES_DIR.toString()
             + File.separator + "timeseriesForecasting" + File.separator + "sample-data"
             + File.separator + "wine.arff";
                
           // load the wine data
           Instances wine = new Instances(new BufferedReader(new FileReader(pathToWineData)));      
                   
           // new forecaster
           WekaForecaster forecaster = new WekaForecaster();
                
           // set the targets we want to forecast. This method calls
           // setFieldsToLag() on the lag maker object for us
           forecaster.setFieldsToForecast("Fortified,Dry-white");
                
           // default underlying classifier is SMOreg (SVM) - we'll use
           // gaussian processes for regression instead
           forecaster.setBaseForecaster(new GaussianProcesses());
                
           forecaster.getTSLagMaker().setTimeStampField("Date"); // date time stamp
           forecaster.getTSLagMaker().setMinLag(1);
           forecaster.getTSLagMaker().setMaxLag(12); // monthly data
                
           // add a month of the year indicator field
           forecaster.getTSLagMaker().setAddMonthOfYear(true);
                
           // add a quarter of the year indicator field
           forecaster.getTSLagMaker().setAddQuarterOfYear(true);
                
           // build the model
           forecaster.buildForecaster(wine, System.out);
                
           // prime the forecaster with enough recent historical data
           // to cover up to the maximum lag. In our case, we could just supply
           // the 12 most recent historical instances, as this covers our maximum
           // lag period
           forecaster.primeForecaster(wine);
                
           // forecast for 12 units (months) beyond the end of the
           // training data
           <<List<List<NumericPrediction>> forecast = forecaster.forecast(12, System.out);
                
                   
                   
           // output the predictions. Outer list is over the steps; inner list is over
           // the targets
           for (int i = 0; i < 12; i++) {
             List<NumericPrediction> predsAtStep = forecast.get(i);
             for (int j = 0; j < 2; j++) {
               NumericPrediction predForTarget = predsAtStep.get(j);
               System.out.print("" + predForTarget.predicted() + " ");
             }
             System.out.println();
           }
                
           // we can continue to use the trained forecaster for further forecasting
           // by priming with the most recent historical data (as it becomes available).
           // At some stage it becomes prudent to re-build the model using current
           // historical data.
        */
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}