def single_model_forecasting(data, lag, h_train, h_test, lr, epoch, batch_size, hidden_num, method, use_cuda): # normalize time series scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(data) trainData, testData = divideTrainTest(dataset) flag = True # using RNN format or not trainX, trainY = create_multi_ahead_samples(trainData, lag, h_train, RNN=flag) testX, testY = create_multi_ahead_samples(testData, lag, h_test, RNN=flag) trainY = np.squeeze(trainY).reshape(-1, h_train) testY = np.squeeze(testY).reshape(-1, h_test) print("train X shape:", trainX.shape) print("train y shape:", trainY.shape) print("test X shape:", testX.shape) print("test y shape:", testY.shape) net = train(trainX, trainY, epoch=epoch, lr=lr, batchSize=batch_size, lag=lag, method=method, hidden_num=hidden_num, use_cuda=use_cuda) testPred = predict_iteration(net, testX, h_test, use_cuda=use_cuda, RNN=flag) # trainPred = predict_iteration(net, trainX, h_train, use_cuda=use_cuda, RNN=flag) # print("train pred shape:", trainPred.shape) print("test pred shape:", testPred.shape) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) # evaluation MAE = eval.calcMAE(testY, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(testY, testPred) print("test RMSE", MRSE) MAPE = eval.calcMAPE(testY, testPred) print("test MAPE", MAPE) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) return testPred, testY
def decompose_ML_forecasting(model, lag, freq, h_train, h_test): # 序列分解 trend, seasonal, residual = ts_decompose(ts, freq) print("ts decomposition is finished!") print("trend shape is", trend.shape) print("season shape is", seasonal.shape) print("residual shape is", residual.shape) # forecasting sub-series independently trend_pred, trend_y = ML_forecasting(trend, model, lag=lag, train_look_ahead=h_train, test_look_ahead=h_test) res_pred, res_y = ML_forecasting(residual, model, lag=lag, train_look_ahead=h_train, test_look_ahead=h_test) season_pred, season_y = ML_forecasting(seasonal, model, lag=lag, train_look_ahead=h_train, test_look_ahead=h_test) trend_pred = trend_pred.reshape(-1, h_test) trend_y = trend_y.reshape(-1, h_test) res_pred = res_pred.reshape(-1, h_test) res_y = res_y.reshape(-1, h_test) season_pred = season_pred.reshape(-1, h_test) season_y = season_y.reshape(-1, h_test) print("trend_pred shape is", trend_pred.shape) print("res_pred shape is", res_pred.shape) print("season_pred shape is", season_pred.shape) print("trend_y shape is", trend_y.shape) print("res_y shape is", res_y.shape) print("season_y shape is", season_y.shape) testPred = trend_pred + res_pred + season_pred testY = trend_y + res_y + season_y # evaluation MAE = eval.calcMAE(testY, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(testY, testPred) print("test RMSE", MRSE) MAPE = eval.calcMAPE(testY, testPred) print("test MAPE", MAPE) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) return testPred, testY
def decomposition_model_forecasting(ts, dataset, lag, h_train, h_test, freq, epoch, lr, batch_size, hidden_num, use_cuda, method): # time series decomposition trend, seasonal, residual = ts_decompose(ts, freq) print("ts decomposition is finished!") print("trend shape is", trend.shape) print("season shape is", seasonal.shape) print("residual shape is", residual.shape) # forecasting sub-series independently print('Trend') trend_pred, trend_y = single_model_forecasting(trend, lag=lag, h_train=h_train, h_test=h_test, epoch=epoch, lr=lr, hidden_num=hidden_num, batch_size=batch_size, method=method, use_cuda=use_cuda) print('Residual') res_pred, res_y = single_model_forecasting(residual, lag=lag, h_train=h_train, h_test=h_test, epoch=epoch, lr=lr, hidden_num=hidden_num, batch_size=batch_size, method=method, use_cuda=use_cuda) print('Season') season_pred, season_y = single_model_forecasting(seasonal, lag=lag, h_train=h_train, h_test=h_test, epoch=epoch, lr=lr, hidden_num=hidden_num, batch_size=batch_size, method=method, use_cuda=use_cuda) trend_pred = trend_pred.reshape(-1, h_test) trend_y = trend_y.reshape(-1, h_test) res_pred = res_pred.reshape(-1, h_test) res_y = res_y.reshape(-1, h_test) season_pred = season_pred.reshape(-1, h_test) season_y = season_y.reshape(-1, h_test) print("trend_pred shape is", trend_pred.shape) print("res_pred shape is", res_pred.shape) print("season_pred shape is", season_pred.shape) print("trend_y shape is", trend_y.shape) print("res_y shape is", res_y.shape) print("season_y shape is", season_y.shape) testPred = trend_pred + res_pred + season_pred testY = trend_y + res_y + season_y # evaluation MAE = eval.calcMAE(testY, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(testY, testPred) print("test RMSE", MRSE) MAPE = eval.calcMAPE(testY, testPred) print("test MAPE", MAPE) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) return testPred, testY
def single_model_forecasting(data, lookBack, epoch, lr, batchSize, method, modelPath, hidden_num, use_cuda): # 归一化数据 scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(data) trainData, testData = divideTrainTest(dataset) flag = True trainX, trainY = createSamples(trainData, lookBack, RNN=flag) testX, testY = createSamples(testData, lookBack, RNN=flag) print("testX shape:", testX.shape) print("testy shape:", testY.shape) print("trainX shape:", trainX.shape) print("trainy shape:", trainY.shape) net = train(trainX, trainY, epoch=epoch, lr=lr, batchSize=batchSize, modelPath=modelPath, lookBack=lookBack, method=method, hidden_num=hidden_num, use_cuda=use_cuda) testPred = predict(testX, net, use_cuda=use_cuda) trainPred = predict(trainX, net, use_cuda=use_cuda) print("train pred shape:", trainPred.shape) print("test pred shape:", testPred.shape) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) MAE = eval.calcMAE(testY, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(testY, testPred) print("test RMSE", MRSE) MAPE = eval.calcMAPE(testY, testPred) print("test MAPE", MAPE) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) return trainPred, testPred, MAE, MRSE, SMAPE
def decomposition_model_forecasting(ts, dataset, lookBack, freq, epoch, lr, batchSize, hidden_num, use_cuda): # 序列分解 trend, seasonal, residual = ts_decompose(ts, freq) print("fcd decompose is finised!") print("trend shape is", trend.shape) print("season shape is", seasonal.shape) print("residual shape is", residual.shape) # 分别预测 MODEL_PATH = "../models/ResRNN_model.pkl" trTrain, trTest, MAE1, MRSE1, SMAPE1 = single_model_forecasting( trend, lookBack=lag, epoch=epoch, lr=lr, use_cuda=use_cuda, batchSize=batchSize, method=METHOD, modelPath=MODEL_PATH, hidden_num=hidden_num) resTrain, resTest, MAE2, MRSE2, SMAPE2 = single_model_forecasting( residual, lookBack=lag, epoch=epoch, lr=lr, use_cuda=use_cuda, batchSize=batchSize, method=METHOD, modelPath=MODEL_PATH, hidden_num=hidden_num) trTrain = trTrain.reshape(-1, 1) trTest = trTest.reshape(-1, 1) resTrain = resTrain.reshape(-1, 1) resTest = resTest.reshape(-1, 1) print("trTrain shape is", trTrain.shape) print("resTrain shape is", resTrain.shape) trendPred, resPred = align(trTrain, trTest, lookBack, resTrain, resTest, lookBack) print("trendPred shape is", trendPred.shape) print("resPred shape is", resPred.shape) # 获取最终预测结果 finalPred = trendPred + seasonal + resPred trainPred = trTrain + seasonal[lookBack:lookBack + trTrain.shape[0]] + resTrain testPred = trTest + seasonal[2 * lookBack + resTrain.shape[0]:] + resTest # 获得ground-truth数据 data = dataset[freq // 2:-(freq // 2)] trainY = data[lookBack:lookBack + trTrain.shape[0]] testY = data[2 * lookBack + resTrain.shape[0]:] print(trainY.shape) print(testY.shape) print(trainPred.shape) print(testPred.shape) # 评估指标 MAE = eval.calcMAE(trainY, trainPred) print("train MAE", MAE) MRSE = eval.calcRMSE(trainY, trainPred) print("train MRSE", MRSE) MAPE = eval.calcMAPE(trainY, trainPred) print("train MAPE", MAPE) MAE = eval.calcMAE(testY, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(testY, testPred) print("test RMSE", MRSE) MAPE = eval.calcMAPE(testY, testPred) print("test MAPE", MAPE) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) return trainPred, testPred, MAE, MRSE, SMAPE
print("trainX shape:", trainX.shape) print("trainy shape:", trainY.shape) groud_truth = [] prediction = [] for i in range(len(testX)): data = testX[i, :].transpose().flatten() model = ExponentialSmoothing(data, trend='add', seasonal='add', seasonal_periods=12) model = model.fit(smoothing_level=None) pred = model.predict(start=0, end=0 + h_test - 1) real_y = testY[i].tolist() groud_truth.extend(pred) prediction.extend(real_y) print("data:", i) print(pred) print(real_y) groud_truth = np.array(groud_truth).reshape(-1, 1) prediction = np.array(prediction).reshape(-1, 1) MAE = eval.calcMAE(groud_truth, prediction) RMSE = eval.calcRMSE(groud_truth, prediction) SMAPE = eval.calcSMAPE(groud_truth, prediction) MAPE = eval.calcMAPE(groud_truth, prediction) print('Test MAE: %.8f' % MAE) print('Test RMSE: %.8f' % RMSE) print("test MAPE", MAPE) print('Test SMAPE: %.8f' % SMAPE)