weka time series forecasting java eclipse
weka time series forecasting java eclipse
Program:
package weka_tsf;
import java.io.*;
import java.util.List;
import weka.core.Instances;
import weka.classifiers.functions.GaussianProcesses;
import weka.classifiers.evaluation.NumericPrediction;
import weka.classifiers.timeseries.WekaForecaster;
import weka.classifiers.timeseries.core.TSLagMaker;
public class weka_tsf {
/**
* Example of using the time series forecasting API. To compile and
* run the CLASSPATH will need to contain:
*
* weka.jar (from your weka distribution)
* pdm-timeseriesforecasting-ce-TRUNK-SNAPSHOT.jar (from the time series package)
* jcommon-1.0.14.jar (from the time series package lib directory)
* jfreechart-1.0.13.jar (from the time series package lib directory)
*/
public static void main(String[] args) {
try {
// path to the Australian wine data included with the time series forecasting
// package
String pathToWineData=””;
try{
pathToWineData = “C:\\Users\\vikas\\Desktop\\temp\\wine.arff”;
}
catch(Exception e)
{}
// 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
try
{
forecaster.buildForecaster(wine, System.out);
}
catch(Exception e)
{
}
// 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() + ” “);
}
}
// 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();
}
System.exit(0);
}
}
My Builld setting:
Properties-> Java Build Path Libraries
pdm-timeseriesforecasting-ce-TRUNK-SNAPSHOT.jar
jcommon-1.0.14.jar
jfreechart-1.0.13.jar
weka-dev-3.7.12.jar
JRE System Library [jre1.8.0_25]
My Run ClassPath settings:
Run As -> Run Configuration->Classpath
Weka.jar // Very Important unless I included this weka 3.7 jar in run path I got an exception saying java.lang.NoClassDefFoundError in buildForecaster method for WekaForecaster class (forecaster object)
pdm-timeseriesforecasting-ce-TRUNK-SNAPSHOT.jar
jcommon-1.0.14.jar
jfreechart-1.0.13.jar
weka-dev-3.7.12.jar
JRE System Library [jre1.8.0_25]
Training Data:
@relation wine2
@attribute Fortified numeric
@attribute Dry-white numeric
@attribute Sweet-white numeric
@attribute Red numeric
@attribute Rose numeric
@attribute Sparkling numeric
@attribute Date date yyyy-MM-dd
@data
2585,1954,85,464,112,1686,1980-01-01
3368,2302,89,675,118,1591,1980-02-01
3210,3054,109,703,129,2304,1980-03-01
3111,2414,95,887,99,1712,1980-04-01
3756,2226,91,1139,116,1471,1980-05-01
4216,2725,95,1077,168,1377,1980-06-01
5225,2589,96,1318,118,1966,1980-07-01
4426,3470,128,1260,129,2453,1980-08-01
3932,2400,124,1120,205,1984,1980-09-01
3816,3180,111,963,147,2596,1980-10-01
3661,4009
,178,996,150,4087,1980-11-01
3795,3924,140,960,267,5179,1980-12-01
…. More
Output predicted:
2477.6980889141087 4189.613759418883
2043.393134064731 4093.6066895410127
1803.1817263518026 4371.464398829972
2533.7046584700224 5197.578820280874
2653.273761538117 5661.193573786854
1031.0125883201963 3081.3850967874832
1479.5731998651056 3845.2706114764896
1863.050867281897 4043.4763834407127
2149.9363842827433 3832.382001078537
2472.507215054707 3749.2031870485794
2666.564247303721 3941.799264982541
3057.92710868181 4373.935356585221