def ANNforecasting(dataset, inputDim, hiddenNum=50, outputDim=1, epoch=20, batchSize=30): # 归一化数 #dataset = dataset.reshape(-1, 1) scaler = MinMaxScaler(feature_range=(0.0, 1.0)) dataset = scaler.fit_transform(dataset) # 分割序列为样本,此处不整理成RNN形式,采用标准形式 train, test = util.divideTrainTest(dataset) trainX, trainY = util.createSamples(train, inputDim, RNN=False) testX, testY = util.createSamples(test, inputDim, RNN=False) print("trainX shape is", trainX.shape) print("trainY shape is", trainY.shape) # 构建模型并训练 ANNModel = ANN.ANNModel(inputDim, hiddenNum, outputDim) t1 = time.time() ANNModel.train(trainX, trainY, epoch, batchSize) t2 = time.time() - t1 print("train time is", t2) # 预测 trainPred = ANNModel.predict(trainX) testPred = ANNModel.predict(testX) # 还原数据 trainPred = scaler.inverse_transform(trainPred) trainY = scaler.inverse_transform(trainY) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) dataset = scaler.inverse_transform(dataset) # 评估指标 # 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) util.plot(trainPred, trainY, testPred, testY) return trainPred, testPred, MAE, MRSE, SMAPE
def MLP_forecasting(dataset, inputDim, lr=1e-3, hiddenNum=50, outputDim=1, epoch=20, batchSize=30, plot_flag=False): # normalize time series # dataset = dataset.reshape(-1, 1) scaler = MinMaxScaler(feature_range=(0.0, 1.0)).fit(dataset) dataset = scaler.transform(dataset) # divide the series into training/testing samples # NOTE: Not RNN format train, test = util.divideTrainTest(dataset) trainX, trainY = util.createSamples(train, inputDim, RNN=False) testX, testY = util.createSamples(test, inputDim, RNN=False) print("trainX shape is", trainX.shape) print("trainY shape is", trainY.shape) print("testX shape is", testX.shape) print("testY shape is", testY.shape) # buil model and train MLP_model = MLP.MLP_Model(inputDim, hiddenNum, outputDim, lr) t1 = time.time() MLP_model.train(trainX, trainY, epoch, batchSize) t2 = time.time() - t1 print("train time is", t2) # forecasting trainPred = MLP_model.predict(trainX) testPred = MLP_model.predict(testX) # reverse the time series trainPred = scaler.inverse_transform(trainPred) trainY = scaler.inverse_transform(trainY) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) # evaluate MAE = eval.calcMAE(testY, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(testY, testPred) print("test RMSE", MRSE) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) if plot_flag: util.plot(trainPred, trainY, testPred, testY) return trainPred, testPred, MAE, MRSE, SMAPE
def statefulRNNforecasting(dataset,lookBack,inputDim = 1,hiddenNum = 100 ,outputDim = 1 ,unit = "GRU",epoch = 50,batchSize = 10,varFlag=False, minLen = 15, maxLen = 30,inputNum = 150): # 归一化数据 scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # 分割序列为样本,并整理成RNN的输入形式 train,test = util.divideTrainTest(dataset) trainX,trainY = util.createSamples(train,lookBack) testX, testY = util.createSamples(test,lookBack) # 构建模型并训练 RNNModel = staRNNs.statefulRNNsModel(inputDim, hiddenNum, outputDim, unit=unit, batchSize=batchSize, lag=lookBack) if varFlag: vtrainX, vtrainY = util.createVariableDataset(train, minLen, maxLen, inputNum) RNNModel.train(vtrainX, vtrainY, epoch, batchSize) else: RNNModel.train(trainX, trainY, epoch, batchSize) # 预测 trainPred = RNNModel.predict(trainX, batchSize) testPred = RNNModel.predict(testX, batchSize) # 还原数据 trainPred = scaler.inverse_transform(trainPred) trainY = scaler.inverse_transform(trainY) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) dataset = scaler.inverse_transform(dataset) # 评估指标 # 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 MAPE", SMAPE) # util.LBtest(testY-testPred) # plt.plot(testY-testPred) # plt.show() #util.plot(trainPred,trainY,testPred,testY) return trainPred, testPred, MAE, MRSE, SMAPE
def run(data, lookBack, train_lookAhead, test_lookAhead, epoch, lr, batchSize, method, modelPath): # 归一化数据 scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(data) # 分割训练测试样本 trainData, testData = divideTrainTest(dataset) # 分割序列为样本, 支持RNN或者普通样本形式 flag = True trainX, trainY = createSamples(trainData, lookBack=lookBack, lookAhead=train_lookAhead, RNN=flag) testX, testY = createSamples(testData, lookBack=lookBack, lookAhead=test_lookAhead, RNN=flag) print("testX shape:", testX.shape) print("testy shape:", testY.shape) print("trainX shape:", trainX.shape) print("trainy shape:", trainY.shape) train(trainX, trainY, epoch=epoch, lr=lr, batchSize=batchSize, modelPath=modelPath, lookBack=lookBack, lookAhead=train_lookAhead, method=method) # testPred = predict(testX, MODEL_PATH) testPred = predict_iteration(testX, test_lookAhead, MODEL_PATH) print("testPred shape:", testPred.shape) # trainPred = predict(trainX, MODEL_PATH) # print("trainPred shape:", trainPred.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) SMAPE = eval.calcSMAPE(testY, testPred) print("test SMAPE", SMAPE) return testPred, MAE, MRSE, SMAPE
def SVRforecasting(dataset, lookBack): # 归一化数 #dataset = dataset.reshape(-1, 1) scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # 分割序列为样本,此处不整理成RNN形式,采用标准形式 train, test = util.divideTrainTest(dataset) trainX, trainY = util.createSamples(train, lookBack, RNN=False) testX, testY = util.createSamples(test, lookBack, RNN=False) # 构建模型并训练 SVRModel = SVR.SVRModel(C=2, epsilon=0.01) SVRModel.train(trainX, trainY) # 预测 trainPred = SVRModel.predict(trainX) testPred = SVRModel.predict(testX) # 还原数据 trainPred = scaler.inverse_transform(trainPred) trainY = scaler.inverse_transform(trainY) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) dataset = scaler.inverse_transform(dataset) # 评估指标 # 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) #util.plot(trainPred,trainY,testPred,testY) return trainPred, testPred, MAE, MRSE, SMAPE
def SVR_forecasting(dataset, lookBack, C=2.0, epsilon=0.01, plot_flag=False): # normalize time series scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # divide the series into training/testing samples # NOTE: Not RNN format train, test = util.divideTrainTest(dataset) trainX, trainY = util.createSamples(train, lookBack, RNN=False) testX, testY = util.createSamples(test, lookBack, RNN=False) print("trainX shape is", trainX.shape) print("trainY shape is", trainY.shape) print("testX shape is", testX.shape) print("testY shape is", testY.shape) # buil model and train SVRModel = SVR.SVRModel(C=C, epsilon=epsilon) SVRModel.train(trainX, trainY) # forecasting trainPred = SVRModel.predict(trainX).reshape(-1, 1) testPred = SVRModel.predict(testX).reshape(-1, 1) # reverse the time series trainPred = scaler.inverse_transform(trainPred) trainY = scaler.inverse_transform(trainY) testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) # evaluate 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) if plot_flag: util.plot(trainPred, trainY, testPred, testY) return trainPred, testPred, MAE, MRSE, SMAPE
def run_aram(data, maxar, maxma): train, test = util.divideTrainTest(data) print("train shape is", train.shape) print("test shape is", test.shape) diffn = 0 if stationarity(train) < 0.01: print('平稳,不需要差分') else: diffn = best_diff(train, maxdiff=8) # train = produce_diffed_timeseries(train, diffn) print('差分阶数为' + str(diffn)) print('开始进行ARMA拟合') order = choose_order(train, maxar, maxma) print('模型的阶数为:' + str(order)) diffn = 1 _ar = order[0] _ma = order[1] train = train.flatten() model = pf.ARIMA(data=train, ar=_ar, ma=_ma, integ=diffn, family=pf.Normal()) model.fit("MLE") testPred = model.predict(len(test)) # testPred = predict_recover(test_predict, train, diffn) # 评估指标 MAE = eval.calcMAE(test, testPred) print("test MAE", MAE) MRSE = eval.calcRMSE(test, testPred) print("test RMSE", MRSE) MAPE = eval.calcMAPE(test, testPred) print("test MAPE", MAPE) SMAPE = eval.calcSMAPE(test, testPred) print("test SMAPE", SMAPE)
def RNN_forecasting(dataset, lookBack, lr, inputDim=1, hiddenNum=64, outputDim=1, unit="GRU", epoch=20, batchSize=30, varFlag=False, minLen=15, maxLen=30, step=5): # 归一化数据 scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # 分割序列为样本,并整理成RNN的输入形式 train, test = util.divideTrainTest(dataset) trainX = None trainY = None vtrainX = None vtrainY = None testX = None testY = None vtestX = None vtestY = None # 构建模型并训练 RNNModel = RNNs.RNNsModel(inputDim, hiddenNum, outputDim, unit, lr) if varFlag: vtrainX, vtrainY = util.createVariableDataset(train, minLen, maxLen, step) vtestX, vtestY = util.createVariableDataset(test, minLen, maxLen, step) print("trainX shape is", vtrainX.shape) print("trainY shape is", vtrainY.shape) print("testX shape is", vtestX.shape) print("testY shape is", vtestY.shape) RNNModel.train(vtrainX, vtrainY, epoch, batchSize) else: trainX, trainY = util.createSamples(train, lookBack) testX, testY = util.createSamples(test, lookBack) print("trainX shape is", trainX.shape) print("trainY shape is", trainY.shape) print("testX shape is", testX.shape) print("testY shape is", testY.shape) RNNModel.train(trainX, trainY, epoch, batchSize) # 预测 if varFlag: trainPred = RNNModel.predictVarLen(vtrainX, minLen, maxLen, step) testPred = RNNModel.predictVarLen(vtestX, minLen, maxLen, step) trainPred = trainPred.reshape(-1, 1) else: trainPred = RNNModel.predict(trainX) testPred = RNNModel.predict(testX) trainPred = trainPred.reshape(-1, 1) if varFlag: # 转化一下test的label testY = util.transform_groundTruth(vtestY, minLen, maxLen, step) testY = testY.reshape(-1, 1) testPred = testPred.reshape(-1, 1) print("testY", testY.shape) print("testPred", 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) #util.plot(trainPred,trainY,testPred,testY) return trainPred, testPred, MAE, MRSE, SMAPE
def diffLagEnsmble(dataset, minLag, maxLag, step, epoch, batchSize=10, inputDim=1, outputDim=1, hiddenNum=50, unit="GRU"): # 归一化数据 dataset = dataset.reshape(-1, 1) scaler = MinMaxScaler() dataset = scaler.fit_transform(dataset) # 分割序列为样本,并整理成RNN的输入形式 train, test = util.divideTrainTest(dataset) modelList = [] #在所有lag下独立地训练若干个RNN for lag in range(minLag, maxLag + 1, step): print("lag is ", lag) trainX, trainY = util.createPaddedDataset(train, lag, maxLag) #testX, testY = util.createPaddedDataset(test, lag, maxLag) RNNModel = RNNs.RNNsModel(inputDim, hiddenNum, outputDim, unit) RNNModel.train(trainX, trainY, epoch, batchSize) modelList.append(RNNModel) print("the number of model is ", len(modelList)) # 变长分割test数据 testX, testY = util.createVariableDataset(test, minLag, maxLag, step) print("testX", testX.shape) print("testY", testY.shape) lagContainNum = (maxLag - minLag) // step + 1 print("lag contains num is ", lagContainNum) # 对每个数据点上不同的lag用不同的模型做预测,目前是简单取平均 testNum = len(testX) testPred = [] for i in range(0, testNum, lagContainNum): predList = [] for j in range(0, lagContainNum): singlePred = modelList[j].predict(testX[i + j:i + j + 1, :, :]) predList.append(singlePred) testPred.append(np.mean(predList)) # 还原数据 testPred = scaler.inverse_transform(testPred) testY = scaler.inverse_transform(testY) testY = util.transformGroundTruth(testY, minLag, maxLag, step) testPred = np.array(testPred) print("testY", testY.shape) print("testPred", testPred.shape) # 评估 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) # plt.plot(testY) # plt.plot(testPred) # plt.show() return testPred, MAE, MRSE, SMAPE
if __name__ == '__main__': p = 6 q = 4 h_test = 1 lag = 24 ts, data = util.load_data("./data/NSW2013.csv", columnName="TOTALDEMAND") # ts, data = util.load_data("./data/bike_hour.csv", columnName="cnt") # ts, data = util.load_data("./data/TAS2016.csv", columnName="TOTALDEMAND") # ts, data = util.load_data("./data/traffic_data_in_bits.csv", columnName="value") # ts, data = util.load_data("./data/beijing_pm25.csv", columnName="pm2.5") # ts, data = util.load_data("./data/pollution.csv", columnName="Ozone") train, test = util.divideTrainTest(data) testX, testY = util.createSamples(test, lookBack=24, RNN=False) testX = testX[:3] print("train shape is", train.shape) print("test shape is", test.shape) train_data = [x[0] for x in train] predictions = [] realTestY = [] for t in range(len(testX)): add_test = [x for x in testX[t]] raw_train_data = train_data train_data.extend(add_test) model = pf.ARIMA(data=np.array(train_data), ar=p,