我是Weka的新手。我有过去10年的每周温度数据集。使用这个数据集,我打算预测明年每周的温度。下面我附上了代码。
import java.io.*;import java.util.List;import weka.core.Instances;import weka.filters.supervised.attribute.TSLagMaker;import weka.classifiers.functions.GaussianProcesses;import weka.classifiers.evaluation.NumericPrediction;import weka.classifiers.timeseries.WekaForecaster;import org.joda.time.*;public class TimeSeriesExample {public static void main(String[] args) { try { // path to data set Instances temp = new Instances(new BufferedReader(new FileReader("sample-data/weeklyMaxTemp.arff"))); // new forecaster WekaForecaster forecaster = new WekaForecaster(); // set the targets to forecast forecaster.setFieldsToForecast("BMxT"); forecaster.setBaseForecaster(new GaussianProcesses()); forecaster.getTSLagMaker().setTimeStampField("Date"); // if there are not enough values in the recent history, return a // negative value indicating the steps to wait if (forecaster.getTSLagMaker().getMaxLag() > temp.size()) { System.out.println("Not enough recent values to make predictions."); } // add a week 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(temp, System.out); forecaster.primeForecaster(temp); // forecast for 52 units (weeks) beyond the end of the training data List<List<NumericPrediction>> forecast = forecaster.forecast(52, System.out); DateTime currentDt = getCurrentDateTime(forecaster.getTSLagMaker()); // output the predictions for (int i = 0; i < 52; ++i) { List<NumericPrediction> predsAtStep = forecast.get(i); for (int j = 0; j < 1; ++j) { NumericPrediction predForTarget = predsAtStep.get(j); System.out.print(currentDt + " ->> " + predForTarget.predicted() + " "); } System.out.println(); currentDt = advanceTime(forecaster.getTSLagMaker(), currentDt); } } catch (Exception ex) { ex.printStackTrace(); }}private static DateTime getCurrentDateTime(TSLagMaker lm) throws Exception { return new DateTime((long) lm.getCurrentTimeStampValue());}private static DateTime advanceTime(TSLagMaker lm, DateTime dt) { return new DateTime((long) lm.advanceSuppliedTimeValue(dt.getMillis()));}}
52表示一年中的周数。
// forecast for 24 units (weeks) beyond the end of the training data List<List<NumericPrediction>> forecast = forecaster.forecast(52, System.out);
当我运行代码时,它给出了52个每周的值。但结果是从训练数据集的最后一个数据的第52周开始的。
这意味着我的训练数据集的最后一天是2015年12月30日。下一个预测值应该是2016年1月6日。但结果数据集是从52周后开始的。
我该如何解决这个问题?
回答:
如下所示进行修改。我们应该先获取当前日期时间。问题解决了
DateTime currentDt = getCurrentDateTime(forecaster.getTSLagMaker()); // forecast units (weeks) beyond the end of the training data List<List<NumericPrediction>> forecast = forecaster.forecast(52, System.out);