Exemple #1
0
def LSTM_forecast(train_x, train_y, valid_x, valid_y, test_x, test_y):
    nodes = test_y.shape[1]
    print(nodes)
    epoch = 200
    batch = 200
    setup_seed(20)
    criterion = nn.MSELoss()
    for i in range(nodes):
        # 得到节点180的训练模型
        train_loader = DataLoader(dataset=TensorDataset(
            train_x[:, i, :, :], train_y[:, i, :]),
                                  batch_size=batch,
                                  shuffle=True)
        lstm_model = FCLSTM(9, 8, 48).to(device)
        optimizer = torch.optim.Adam(lstm_model.parameters(), lr=1e-3)
        for e in range(epoch):
            for _, data in enumerate(train_loader):
                inputs, target = data
                if cuda_gpu:
                    inputs, target = Variable(inputs).to(device), Variable(
                        target).to(device)
                else:
                    inputs, target = Variable(inputs), Variable(target)
                y_hat = lstm_model(inputs)
                loss = criterion(y_hat, target)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
            if (e + 1) % 10 == 0:
                # print(device)
                valid = valid_x[:, i, :, :].to(device)

                print('Epoch [{}/{}], Loss:{:.4f}'.format(
                    e + 1, epoch, loss.item()))
                valid_outputs = lstm_model(valid)
                valid_outputs = valid_outputs.cpu()
                valid_outputs = valid_outputs * (y_max - y_min) + y_min
                Valid_y = valid_y * (y_max - y_min) + y_min
                rmse = measure.GetRMSE(valid_outputs, Valid_y[:, i, :])
                print("valid RMSE:", rmse)

        torch.save(lstm_model, 'FC_LSTM.pkl')
        print('over')
        test = test_x[:, i, :, :].to(device)
        test_outputs = lstm_model(test)
        if i == 0:
            result = test_outputs
        else:
            result = torch.cat((result, test_outputs), dim=1)
    result = result.cpu() * (y_max - y_min) + y_min
    result = result.view(test_y.shape)
    test_y = test_y * (y_max - y_min) + y_min
    print("RMSE: ", measure.GetRMSE(test_y, result))
    print("MAP: ", measure.GetMAP(test_y, result))
    print("SKILL: ", measure.Skill(test_y, result))
    print("MAPE: ", measure.GetMAPE(test_y, result))
    print("R2: ", measure.R_square(test_y, result))
Exemple #2
0
def SVR_forecast(train_x, train_y, test_x, test_y, pre_len):
    nodes = test_y.shape[1]
    print(nodes)
    result = []
    for i in range(nodes):
        a_X = np.array(train_x[:, i, :])
        print(a_X.shape)
        a_Y = np.array(train_y[:, i, 0])
        t_X = np.array(test_x[:, i, :])
        print(t_X.shape)
        svr_model = GridSearchCV(SVR(),
                                 param_grid={
                                     "kernel": ("linear", 'rbf', 'sigmoid'),
                                     "C": np.logspace(-3, 3, 7),
                                     "gamma": np.logspace(-3, 3, 7)
                                 },
                                 cv=5)
        # svr_model = SVR('rbf')
        svr_model.fit(a_X, a_Y)
        for i in range(pre_len):
            if i == 0:
                pre = svr_model.predict(t_X)
                Predict = pre.reshape(-1, 1)
                # print("Predict: ", Predict.shape)
            else:
                t_X = np.concatenate((t_X[:, 1:], pre.reshape(-1, 1)), axis=1)
                pre = svr_model.predict(t_X)
                Predict = np.concatenate((Predict, pre.reshape(-1, 1)), axis=1)
        print(Predict.shape)
        result.append(Predict)
    result = np.array(result)
    result = torch.tensor(result).permute(1, 0, 2) * (y_max - y_min) + y_min
    test_y = test_y * (y_max - y_min) + y_min
    print("RMSE: ", measure.GetRMSE(test_y, result))
    print("MAP: ", measure.GetMAP(test_y, result))
    print("SKILL: ", measure.Skill(test_y, result))
    print("MAPE: ", measure.GetMAPE(test_y, result))
    print("R2: ", measure.R_square(test_y, result))
    print(result.shape)
Exemple #3
0
def HA_forecasr(test_x, test_y, pre_len):
    for i in range(len(test_x)):
        a = test_x[i]
        a1 = torch.mean(a, dim=1)
        for j in range(pre_len):
            if j == 0:
                temp = a1.view(-1, 1)
            else:
                temp = torch.cat((temp, a1.view(-1, 1)), dim=1)
        temp = temp.unsqueeze(0)
        if i == 0:
            result = temp
        else:
            result = torch.cat((result, temp), dim=0)
    print("result.shape: ", result.shape)
    result = result * (y_max - y_min) + y_min
    test_y = test_y * (y_max - y_min) + y_min
    result1 = torch.tensor(result, dtype=torch.float32)
    print("RMSE: ", measure.GetRMSE(test_y, result1))
    print("MAP: ", measure.GetMAP(test_y, result1))
    print("SKILL: ", measure.Skill(test_y, result1))
    print("MAPE: ", measure.GetMAPE(test_y, result))
    print("R2: ", measure.R_square(test_y, result))
Exemple #4
0
def ARIMA_forecast(data, order=[2, 0, 0]):
    dta = pd.DataFrame(data)
    rng = pd.date_range('1/1/2015', periods=5848, freq='3h')
    dta.index = pd.DatetimeIndex(rng)
    RMSE = []
    MAP = []
    MAPE = []
    R2 = []
    for i in range(data.shape[1]):
        ts = dta.iloc[:, i]
        ts_log = np.log(ts)
        ts_log = np.array(ts_log, dtype=np.float)
        where_are_inf = np.isinf(ts_log)
        ts_log[where_are_inf] = 0
        ts_log = pd.Series(ts_log)
        ts_log.index = pd.DatetimeIndex(rng)
        model = ARIMA(ts_log, order=order)
        properModel = model.fit()
        predict_ts = properModel.predict(4971, dynamic=True)
        log_recover = np.exp(predict_ts)
        ts = ts[log_recover.index]
        RMSE.append(
            measure.GetRMSE(torch.from_numpy(ts.values),
                            torch.from_numpy(log_recover.values)))
        MAP.append(
            measure.GetMAP(torch.from_numpy(ts.values),
                           torch.from_numpy(log_recover.values)))
        MAPE.append(
            measure.GetMAPE(torch.from_numpy(ts.values),
                            torch.from_numpy(log_recover.values)))
        R2.append(
            measure.R_square(torch.from_numpy(ts.values),
                             torch.from_numpy(log_recover.values)))
    print("RMSE: ", np.mean(RMSE))
    print('MAP: ', np.mean(MAP))
    print('MAPE: ', np.mean(MAPE))
    print('R2: ', np.mean(R2))
Exemple #5
0
def test(model, his_step, pre_step, x, y):
    print("device: ", device)
    feature_num = x.shape[-1]
    nodes = x.shape[1]
    x_ones, x_min, x_max = measure.Norm(np.array(torch.tensor(x).view(-1, feature_num), dtype=float), dim=2)
    y_ones, y_min, y_max = measure.Norm(np.array(torch.tensor(y).view(-1), dtype=float), dim=1)
    source_x = torch.tensor(x_ones).view(-1, nodes, feature_num)
    source_y = torch.tensor(y_ones).view(-1, nodes)
    hist_x, fore_y = Divide(source_x, source_y, his_step, pre_step)
    _, _, _, _, test_x, test_y = TrainValidTest(hist_x, fore_y, valid=0.05)
    model = model.to(device)
    with torch.no_grad():
        model = model.eval()
        test_x = test_x.to(device)
        test_outputs = model(test_x)
        test_outputs = test_outputs.cpu()
        test_y = test_y * (y_max - y_min) + y_min
        test_outputs = test_outputs * (y_max - y_min) + y_min
    rmse = measure.GetRMSE(test_outputs, test_y)
    mae = measure.GetMAP(test_outputs, test_y)
    skill = measure.Skill(test_outputs, test_y)
    print("RMSE:", rmse)
    print("MAE:", mae)
    print("SKILL:", skill)
    for i in range(pre_step):
        print('step ', i)
        to = test_outputs[:, :, i]
        ty = test_y[:, :, i]
        rmse = measure.GetRMSE(ty, to)
        mae = measure.GetMAP(ty, to)
        mape = measure.GetMAPE(ty, to)
        r2 = measure.R_square(ty, to)
        print("RMSE:", rmse)
        print("MAE:", mae)
        print("MAPE:", mape)
        print("R2:", r2)