我是weka的新手,我想用历史数据来预测未来的价值。为此,我在java中使用wekaapis-wekaforecaster,但我发现很难找到基于多个选定目标进行预测的api。我可以在wekaexplorer中这样做,但不知道如何在java中使用wekaapi。
这是weka和3个选定属性的面板:
预测是基于所有三个属性进行的,并显示为单独的结果,我可以在java中做同样的事情,但它只包含一个属性,不考虑其他两个属性,因此结果不太准确。
这是我的密码。。。
import java.io.BufferedReader;
import java.io.FileReader;
import org.joda.time.DateTime;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.timeseries.WekaForecaster;
import weka.core.Instance;
import weka.core.Instances;
import weka.filters.supervised.attribute.TSLagMaker;
public class ForecastStack {
public static void main(String args[]) throws Exception {
Forecast f = new Forecast();
f.predictTemp();
}
public void predictTemp() throws Exception {
Instances data = new Instances(new BufferedReader(
new FileReader("C:\\Users\\n.lukic\\OneDrive\\Desktop\\Laooconte\\Dati\\arff\\weather.arff")));
data.setClassIndex(data.numAttributes() - 3); // radim uzorak na temp con index = data.length-3
// build model
LinearRegression rd = new LinearRegression();
rd.buildClassifier(data);
Evaluation eval = new Evaluation(data);
eval.evaluateModel(rd, data);
System.out.println("\n");
System.out.println("Mean absolute error = " + eval.meanAbsoluteError());
System.out.println("Root mean squared error = " + eval.rootMeanSquaredError());
System.out.println("\n");
WekaForecaster forecaster = new WekaForecaster();
forecaster.setFieldsToForecast("Temperature");
forecaster.setBaseForecaster(rd);
forecaster.getTSLagMaker().setRemoveLeadingInstancesWithUnknownLagValues(true);
forecaster.getTSLagMaker().setTimeStampField("Date"); // date time stamp
forecaster.getTSLagMaker().setMinLag(1);
forecaster.getTSLagMaker().setMaxLag(12); // monthly data
forecaster.getTSLagMaker().setAddMonthOfYear(true);
forecaster.getTSLagMaker().setAddQuarterOfYear(true);
forecaster.buildForecaster(data, System.out);
DateTime currentDt = getCurrentDateTime(forecaster.getTSLagMaker());
// output model
System.out.println("Date ActTemperature PredTemperature Difference");
for (int i = 0; i < data.numInstances(); i++) {
double actualValue = data.instance(i).classValue();
Instance newInst = data.instance(i);
double predSMO = rd.classifyInstance(newInst);
currentDt = advanceTime(forecaster.getTSLagMaker(), currentDt);
double diff = predSMO - actualValue;
System.out.println(currentDt + ", " + actualValue + ", " + predSMO + " " + diff);
}
}
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()));
}
}
这是arff文件weather.arff:
@relation weather
@attribute Pressure numeric
@attribute Temperature numeric
@attribute Humidity numeric
@attribute Date date 'yyyy-MM-dd'
@data
1040,0,50,2018-01-01
1033,10,40,2018-02-01
1030,14,40,2018-03-01
1033,15,60,2018-04-01
1020,23,38,2018-05-01
1010,29,36,2018-06-01
1000,30,40,2018-07-01
999,37,45,2018-08-01
970,28,50,2018-09-01
1000,19,68,2018-10-01
1030,15,73,2018-11-01
1040,5,70,2018-12-01
1038,2,82,2019-01-01
1035,6,69,2019-02-01
1030,10,55,2019-03-01
1025,14,50,2019-04-01
1022,21,49,2019-05-01
1000,29,44,2019-06-01
995,34,39,2019-07-01
990,33,49,2019-08-01
999,30,52,2019-09-01
1010,18,60,2019-10-01
1030,8,84,2019-11-01
1033,3,67,2019-12-01
1035,0,88,2020-01-01
1030,4,76,2020-02-01
1028,19,56,2020-03-01
1025,22,55,2020-04-01
1022,27,46,2020-05-01
1010,31,36,2020-06-01
1005,34,50,2020-07-01
1000,38,65,2020-08-01
1004,30,66,2020-09-01
1015,23,72,2020-10-01
1025,10,72,2020-11-01
1040,4,82,2020-12-01
有人能帮我吗?任何建议都是有用的。先谢谢你。
暂无答案!
目前还没有任何答案,快来回答吧!