使用Weka预测明年每周的温度值

我是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);

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注