gpt4 book ai didi

r - 如何优化批量预测

转载 作者:行者123 更新时间:2023-12-03 14:37:52 24 4
gpt4 key购买 nike

我遇到了约瑟夫欧文代码 here用于批量预测。我有一个包含接近 19k 行的数据集,但问题是即使应用了批量预测方法,我的代码仍然运行速度非常慢。
在进行实际预测之前,我需要评估使用 MAPE 作为评估标准的最佳模型。下面是相同的可行代码片段。我的问题是如何优化以下代码以使其在可接受的时间内运行(2 分钟以内)

 fcnChooseETS <- function(Ts){

TsPositive <- ( min( as.numeric(Ts) ) > 0 ) # Check if all values of timeseries are positive or not

ModelsUsed <- c("ANN","MNN","ANA","AAN","AAA","MAA","MNM","MMN","MMM","MNA","MAN","MAM")
ModelsNonPositive <- c("ANN","ANA","AAN","AAA") # Multiplicative models cannot take non positive data

if( !TsPositive ){
ModelsUsed <- ModelsNonPositive
}

lAllModels <- lapply(ModelsUsed, function(M){
ets(Ts, damped = NULL, model = M)
})

vecResult <- sapply(lAllModels, function(M) accuracy(M)[2])

names(vecResult) <- ModelsUsed
min(vecResult)
}

fcnTrending <- function( dt){
Ts <- lapply(transpose(dt), ts , frequency = 12 , end = FeedDate)
fit <- lapply(Ts , fcnChooseETS )
}

最佳答案

以下脚本测试了拟合问题中模型的 3 种不同方法。其中第一个是问题中发布的代码的更惯用版本,接下来的两个并行拟合多个模型。
此脚本保存在文件 so_62497397.R 中并运行如下。

#
# filename: so_62497397.R
# Test serial and two types of parallel execution of
# exponential smoothing time series fitting.

library(parallel)
library(foreach)
library(doParallel)
library(forecast)

fcnChooseETS <- function(Ts){

TsPositive <- ( min( as.numeric(Ts) ) > 0 ) # Check if all values of timeseries are positive or not

ModelsUsed <- c("ANN","MNN","ANA","AAN","AAA","MAA","MNM","MMN","MMM","MNA","MAN","MAM")
ModelsNonPositive <- c("ANN","ANA","AAN","AAA") # Multiplicative models cannot take non positive data

if( !TsPositive ){
ModelsUsed <- ModelsNonPositive
}

lAllModels <- lapply(ModelsUsed, function(M){
ets(Ts, damped = NULL, model = M)
})

vecResult <- sapply(lAllModels, function(M) accuracy(M)[2])

names(vecResult) <- ModelsUsed
vecResult[which.min(vecResult)]
}
fcnChooseETS2 <- function(Ts, Ncpus = 2){

TsPositive <- ( min( as.numeric(Ts) ) > 0 ) # Check if all values of timeseries are positive or not

ModelsUsed <- c("ANN","MNN","ANA","AAN","AAA","MAA","MNM","MMN","MMM","MNA","MAN","MAM")
ModelsNonPositive <- c("ANN","ANA","AAN","AAA") # Multiplicative models cannot take non positive data

if( !TsPositive ){
ModelsUsed <- ModelsNonPositive
}

vecResult <- mclapply(ModelsUsed, function(M){
fit <- ets(Ts, damped = NULL, model = M)
accuracy(fit)[2]
}, mc.cores = Ncpus)

vecResult <- unlist(vecResult)
names(vecResult) <- ModelsUsed
vecResult[which.min(vecResult)]
}

fcnChooseETS3 <- function(Ts, Ncpus = 2){

TsPositive <- ( min( as.numeric(Ts) ) > 0 ) # Check if all values of timeseries are positive or not

ModelsUsed <- c("ANN","MNN","ANA","AAN","AAA","MAA","MNM","MMN","MMM","MNA","MAN","MAM")
ModelsNonPositive <- c("ANN","ANA","AAN","AAA") # Multiplicative models cannot take non positive data

if( !TsPositive ){
ModelsUsed <- ModelsNonPositive
}

cl <- makeCluster(Ncpus)
clusterExport(cl, 'ts')
clusterEvalQ(cl, library(forecast))
vecResult <- parLapply(cl, ModelsUsed, function(M){
fit <- ets(Ts, damped = NULL, model = M)
accuracy(fit)[2]
})
stopCluster(cl)

vecResult <- unlist(vecResult)
names(vecResult) <- ModelsUsed
vecResult[which.min(vecResult)]
}

makeTestdata <- function(N){
n <- length(USAccDeaths)
m <- ceiling(log2(N/n))
x <- as.numeric(USAccDeaths)
for(i in seq_len(m)) x <- c(x, x)
L <- length(x)/12 - 1
x <- ts(x, start = 2000 - L, frequency = 12)
x
}


numCores <- detectCores()
cat("numCores:", numCores, "\n")

x <- makeTestdata(5e3)

t1 <- system.time(
res1 <- fcnChooseETS(x)
)
t2 <- system.time(
res2 <- fcnChooseETS2(x, Ncpus = numCores)
)
t3 <- system.time(
res3 <- fcnChooseETS3(x, Ncpus = numCores)
)

rbind(t.lapply = t1,
t.mclapply = t2,
t.parLapply = t3)

c(res1, res2, res3)

运行 Rscript
  • 老化的 PC,处理器 Intel® Core™ i3 CPU 540 @ 3.07GHz × 4 核,
  • R 版本 4.0.2 (2020-06-22)
  • Ubuntu 20.04。

  • 时间显示 mclapply是最好的选择,虽然不比 parLapply 快多少.在拟合模型中,使用 MAPE 选择的模型都应该是相同的。
    rui@rui:~$ Rscript --vanilla so_62497397.R
    #Loading required package: iterators
    #Registered S3 method overwritten by 'quantmod':
    # method from
    # as.zoo.data.frame zoo
    #numCores: 4
    # user.self sys.self elapsed user.child sys.child
    #t.lapply 56.505 0.063 57.389 0.000 0.00
    #t.mclapply 0.039 0.024 33.983 30.506 0.26
    #t.parLapply 0.040 0.012 36.317 0.001 0.00
    # ANA ANA ANA
    #263.0876 263.0876 263.0876

    关于r - 如何优化批量预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62497397/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com