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
Beispiel #5
0
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
Beispiel #7
0
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
Beispiel #8
0
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,
                         ma=q,