我是 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周之后开始。
我该如何解决这个问题?