Dan*_*iel 6 statistics r machine-learning prediction sliding-window
我正在努力解决一个问题。我正在使用 SparkR 进行时间序列预测,但是这个场景也可以转移到普通的 R 环境中。我想使用回归模型(如随机森林回归等)来预测未来一天的负载,而不是使用 ARIMA 模型。我还阅读了滑动窗口方法来评估不同回归器相对于不同参数组合的性能。所以为了更好地理解这是我的数据集结构的一个例子:
Timestamp UsageCPU UsageMemory Indicator Delay
2014-01-03 21:50:00 3123 1231 1 123
2014-01-03 22:00:00 5123 2355 1 322
2014-01-03 22:10:00 3121 1233 2 321
2014-01-03 22:20:00 2111 1234 2 211
2014-01-03 22:30:00 1000 2222 2 0
2014-01-03 22:40:00 4754 1599 1 0
Run Code Online (Sandbox Code Playgroud)
要使用任何类型的回归器,下一步是提取特征并将它们转换为可读格式,因为这些回归无法读取时间戳:
Year Month Day Hour Minute UsageCPU UsageMemory Indicator Delay
2014 1 3 21 50 3123 1231 1 123
2014 1 3 22 00 5123 2355 1 322
2014 1 3 22 10 3121 1233 2 321
2114 1 3 22 20 2111 1234 2 211
Run Code Online (Sandbox Code Playgroud)
下一步是为模型创建训练和测试集。
trainTest <-randomSplit(SparkDF,c(0.7,0.3), seed=42)
train <- trainTest[[1]]
test <- trainTest[[2]]
Run Code Online (Sandbox Code Playgroud)
那么就可以创建模型+预测(randomForest的设置首先是不相关的):
model <- spark.randomForest(train, UsageCPU ~ ., type = "regression", maxDepth = 5, maxBins = 16)
predictions <- predict(model, test)
Run Code Online (Sandbox Code Playgroud)
所以我知道所有这些步骤,通过用实际数据绘制预测数据,它看起来非常好。但是这个回归模型不是动态的,这意味着我无法提前一天预测。因为UsageCPU、UsageMemory等特征不存在,我想从历史值预测到第二天。正如开头提到的,滑动窗口方法可以在这里工作,但我不确定如何应用它(在整个数据集上,仅在训练或测试集上)。
这个实现来自shabbychef's和mbq:
slideMean<-function(x,windowsize=3,slide=2){
idx1<-seq(1,length(x),by=slide);
idx1+windowsize->idx2;
idx2[idx2>(length(x)+1)]<-length(x)+1;
c(0,cumsum(x))->cx;
return((cx[idx2]-cx[idx1])/windowsize);
}
Run Code Online (Sandbox Code Playgroud)
最后一个问题涉及窗口大小。我想以小时为单位预测第二天(00,01,02,03 ...),但时间戳的间隔为 10 分钟,因此在我的计算中,窗口的大小应为 144(10*60*24 / 10)。
如果有人可以帮助我,那就太好了。谢谢!
使用神经网络进行时间序列预测时我也遇到了同样的问题。我实现了许多模型,效果最好的一个是滑动窗口与神经网络相结合。我也从该领域的其他研究人员那里得到了证实。由此我们得出的结论是,如果您想在一步中预测提前 1 天(24 个视野),则对系统的训练要求很高。我们进行了以下操作:
1. We had a sliding window of 24 hours. e.g lets use [1,2,3] here
2. Then use ML model to predict the [4]. Meaning use value 4 as target.
# As illustration we had
x = [1,2,3]
# then set target as
y=[4].
# We had a function that returns the x=[1,2,3] and y =[4] and
# shift the window in the next training step.
3.To the:
x =[1,2,3]
we can add further features that are important to the model.
x=[1,2,3,feature_x]
4. Then we minimise error and shift the window to have:
x = [2,3,4,feature_x] and y = [5].
5. You could also predict two values ahead. e.g [4,5] .
6. Use a list to collect output and plot
7. Make prediction after the training.
Run Code Online (Sandbox Code Playgroud)