def preprocess_data(x, y, feature_num, seq_len, pre_len): nodes = x.shape[1] if feature_num == 1: x_ones, x_min, x_max = measure.Norm(np.array(torch.tensor(x).view(-1), dtype=float), dim=1) source_x = torch.tensor(x_ones).view(-1, nodes) else: x_ones, x_min, x_max = measure.Norm(np.array(torch.tensor(x).view( -1, feature_num), dtype=float), dim=2) source_x = torch.tensor(x_ones).view(-1, nodes, feature_num) global y_min global y_max y_ones, y_min, y_max = measure.Norm(np.array(torch.tensor(y).view(-1), dtype=float), dim=1) source_y = torch.tensor(y_ones).view(-1, nodes) hist_x, fore_y = Divide(source_x, source_y, seq_len, pre_len) return TrainValidTest(hist_x, fore_y, valid=0.05)
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)
def ImportantInRF(x, y): print(x.shape) feature_num = x.shape[-1] nodes = x.shape[1] print(feature_num) x, _, _ = measure.Norm(np.array(torch.tensor(x).view(-1, feature_num)), dim=2) x = x.reshape(-1, nodes, feature_num) x, y = su.shuffle(x, y, random_state=0) train_size = int(len(x) * 0.9) score = None importances = None for i in range(x.shape[1]): start = time.time() x_city = x[:, i, :] y_city = y[:, i] train_x, test_x, train_y, test_y = x_city[:train_size], x_city[train_size:], \ y_city[:train_size], y_city[train_size:] model = se.RandomForestRegressor(max_depth=10, n_estimators=1000, min_samples_split=3) model.fit(train_x, train_y) # 模型测试 pred_test_y = model.predict(test_x) if i == 0: score = sm.r2_score(test_y, pred_test_y) importances = model.feature_importances_ else: importances += model.feature_importances_ score += sm.r2_score(test_y, pred_test_y) end = time.time() print('time: ', end - start) importances = importances / x.shape[1] score = score / x.shape[1] print('score: ', score) sorted_indexes = importances.argsort()[::-1] print(importances) print(attribute[sorted_indexes])
def Forecast(his_step, fore_step, x, y, lack=None): # x [num, city, feature] # y [num, city] RSME = [] MAE = [] SKILL = [] feature_num = x.shape[2] num_hidden = 48 num_layers = 1 encoder = model_fs.EncoderGRU(feature_num, num_hidden, num_layers, device=device) decoder = model_fs.AttnDecoderRNN(feature_num, num_hidden, output_size=1) if cuda_gpu: encoder = encoder.to(device) decoder = decoder.to(device) enandde = model_fs.EncoderDecoderAtt(encoder=encoder, decoder=decoder, time_step=fore_step) criterion = nn.MSELoss() if cuda_gpu: enandde = enandde.to(device) criterion = criterion.to(device) optimizer = torch.optim.Adam(enandde.parameters(), lr=1e-2) for city in range(x.shape[1]): x_ones, _, _ = measure.Norm(x[:, city, :], 2) y_ones, y_min, y_max = measure.Norm(y[:, city], 1) hist_x, fore_y = Divide(his_step, fore_step, x_ones, y_ones) batch_size = 100 epoch = 100 train_x, train_y, test_x, test_y = TrainValidTest(hist_x, fore_y) train_loader = DataLoader(dataset=TensorDataset(train_x, train_y), batch_size=batch_size, shuffle=True, num_workers=4) for e in range(epoch): for i, 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) # print("epoch:", e, i, "inputs:", inputs.shape, "target:", target.shape) outputs, attn_weights = enandde(inputs) loss = criterion(outputs, target) optimizer.zero_grad() loss.backward() optimizer.step() if e % epoch == 0 or e + 1 == epoch: print('Epoch [{}/{}], Loss:{:.4f}'.format( e + 1, epoch, loss.item())) with torch.no_grad(): test_x = test_x.to(device) test_outputs, _ = enandde(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) RSME.append(rmse) MAE.append(mae) SKILL.append(skill) SaveNpy(RSME, "RMSE" + str(lack)) SaveNpy(MAE, "MAE" + str(lack)) SaveNpy(SKILL, "SKILL" + str(lack))
def train(self): print("device: ", self.device) train_loss = [] valid_rsme = [] nodes = self.x.shape[1] feature_num = self.x.shape[-1] criterion = nn.MSELoss() if cuda_gpu: model = self.model.to(device) criterion = criterion.to(device) model = model.train() optimizer = torch.optim.Adam(model.parameters(), lr=self.lr, weight_decay=self.decay) x_ones, x_min, x_max = measure.Norm(np.array(torch.tensor(self.x).view( -1, feature_num), dtype=float), dim=2) y_ones, y_min, y_max = measure.Norm(np.array(torch.tensor( self.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, self.his, self.pre) train_x, train_y, valid_x, valid_y, test_x, test_y = TrainValidTest( hist_x, fore_y, train=self.trainset, valid=0.05) setup_seed(20) train_loader = DataLoader(dataset=TensorDataset(train_x, train_y), batch_size=self.batch, shuffle=True) Valid_y = valid_y * (y_max - y_min) + y_min for e in range(self.epoch): for i, 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 = model(inputs) loss = criterion(y_hat, target) optimizer.zero_grad() loss.backward() optimizer.step() if (e + 1) % 10 == 0: # print(device) print('Epoch [{}/{}], Loss:{:.4f}'.format( e + 1, self.epoch, loss.item())) train_loss.append(loss.item()) with torch.no_grad(): valid_x = valid_x.to(device) MyModel = MyModel.eval() valid_outputs = MyModel(valid_x) valid_outputs = valid_outputs.cpu() valid_outputs = valid_outputs * (y_max - y_min) + y_min rmse = measure.GetRMSE(valid_outputs, Valid_y) valid_rsme.append(rmse) # mae = Measure.GetMAP(valid_outputs, Valid_y) # skill = Measure.Skill(valid_outputs, Valid_y) # valid_rmse.append(rmse) print("valid RMSE:", rmse) # print("valid MAE:", mae) # print("valid SKILL:", skill) model = model.train() np.save("valid_RSME" + model.name, np.array(valid_rsme)) np.save("train_Loss" + model.name, np.array(train_loss)) if self.valid is not None: print('valid result: ', rmse) return rmse, model.cpu() with torch.no_grad(): model = model.eval() test_x = test_x.to(device) # print(test_y.shape) # test_outputs = T_GCN(test_x) test_outputs = MyModel(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_y, test_outputs) # mae = Measure.GetMAP(test_y, test_outputs) # mape = Measure.GetMAPE(test_y, test_outputs) # r2 = Measure.R_square(test_y, test_outputs) print("RMSE:", rmse) # print("MAE:", mae) # print("MAPE:", mape) # print("R2:", r2) # print(train_loss) # print(valid_rsme) return rmse, model.cpu()