首页 > 解决方案 > 如何使用 h2o.predict 预测时间序列的未来值

问题描述

我正在阅读“Hands-on Time series analysis with R”一书,我被困在使用机器学习 h2o 包的示例中。我不知道如何使用 h2o.predict 函数。在示例中,它需要 newdata 参数,在这种情况下是测试数据。但是,如果您实际上不知道这些值,您如何预测时间序列的未来值?

如果我只是忽略 newdata 参数,我会得到:缺少newdata参数的预测尚未实现。

library(h2o)

h2o.init(max_mem_size = "16G")


train_h <- as.h2o(train_df)
test_h <- as.h2o(test_df)
forecast_h <- as.h2o(forecast_df)


x <- c("month", "lag12", "trend", "trend_sqr")
y <- "y"

rf_md <- h2o.randomForest(training_frame = train_h,
                          nfolds = 5,
                          x = x,
                          y = y,
                          ntrees = 500,
                          stopping_rounds = 10,
                          stopping_metric = "RMSE",
                          score_each_iteration = TRUE,
                          stopping_tolerance = 0.0001,
                          seed = 1234)

h2o.varimp_plot(rf_md)

rf_md@model$model_summary

library(plotly)

tree_score <- rf_md@model$scoring_history$training_rmse
plot_ly(x = seq_along(tree_score), y = tree_score,
        type = "scatter", mode = "line") %>%
  layout(title = "Random Forest Model - Trained Score History",
         yaxis = list(title = "RMSE"),
         xaxis = list(title = "Num. of Trees"))

test_h$pred_rf <- h2o.predict(rf_md, test_h)

test_1 <- as.data.frame(test_h)

mape_rf <- mean(abs(test_1$y - test_1$pred_rf) / test_1$y)
mape_rf

标签: rmachine-learningh2o

解决方案


H2O-3 不支持传统的时间序列算法(例如,ARIMA)。相反,建议将时间序列用例视为监督学习问题并执行时间序列特定的预处理。

例如,如果您的目标是预测明天一家商店的销售额,您可以将其视为一个回归问题,其中您的目标是销售额。但是,如果您尝试在原始数据上训练监督学习模型,那么您的表现可能会很差。所以诀窍是添加历史属性,如滞后作为预处理步骤。

如果我们在未更改的数据集上训练模型,平均绝对误差约为 35%。

在此处输入图像描述

如果我们开始添加该商店前一天的销售额等历史特征,我们可以将平均绝对误差降低到 15% 左右。

在此处输入图像描述

虽然 H2O-3 不支持滞后,但您可以利用苏打水来执行此预处理。您可以使用 Spark 生成每组的滞后,然后使用 H2O-3 训练回归模型。这是此过程的示例:https ://github.com/h2oai/h2o-tutorials/tree/master/best-practices/forecasting


推荐阅读