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

标签 java machine-learning 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.01.06。但结果数据集是从 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);

关于java - 使用 Weka 每周​​预测明年的温度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58537153/

相关文章:

java - 使用 stax2-api 的问题

python - 在 Dataframe 中的某些列上输入

python - 寻找影响净收入的特征

python-3.x - 使用 MNIST 加载数据集但出现文件未找到错误,Windows 10,Python 3

nlp - 如何使用 Weka 创建词袋?

java - WEKA - 使用 api 时出现 java.lang.IndexOutOfBoundsException 错误

java - Google App Engine 上的 Metro 网络服务

Java Collection-ArrayList 和 Vector 之间的加速

java - 如何屏蔽 Protobuf 中的某些字段

Java - WEKA - 向训练集添加新类别