def TrainTestFull(X, y, Names, TestName, lons, lats, lons1, lats1, area_flat, plot_dir): (X_train, X_test, y_train, y_test, names_train, names_test) = split_set(X, y, Names, TestName) # Train (m, kern) = TrainModel(X_train, y_train) # Predict y_pred, cov = TestModel(m, X_test, y_test) # Plot PlotMap(y_test, y_pred, lons, lats, names_train, names_test, plot_dir, area_flat) regions_all = ['Global'] + list(RegionsList) print(y_pred.shape) for v in range(len(TestName)): Validation(y_test[v:v + 1, ], y_pred[v:v + 1, :], cov[v:v + 1, :], lons, lats, lons1, lats1, names_train, names_test, plot_dir + TestName[v] + '_', area_flat, metric_regions=regions_all) Save(names_train, names_test, X_train, X_test, y_train, y_test, y_pred, lons, lats, lons1, lats1, area_flat, m, kern, plot_dir + TestName[0] + '_')
def TrainTestPCA(X, y, Names, TestName, lons, lats, lons1, lats1, area_flat, plot_dir, n_comp=None, pca_inputs=True, pca_outputs=True): (X_train, X_test, y_train, y_test, names_train, names_test) = split_set(X, y, Names, TestName) # PCA if pca_inputs is True: X_PCA = PCA(n_comp).fit(X_train) X_trans = X_PCA.transform(X_train) X_test_trans = X_PCA.transform(X_test) else: X_trans = X_train X_test_trans = X_test if pca_outputs is True: y_PCA = PCA(n_comp).fit(y_train) y_trans = y_PCA.transform(y_train) y_test_trans = y_PCA.transform(y_test) else: y_trans = y_train y_test_trans = y_test # Train (m, kern) = TrainModel(X_trans, y_trans) y_pred, cov = TestModel(m, X_test_trans, y_test_trans) # Inverse PC if pca_outputs is True: y_pred_full = y_PCA.inverse_transform(y_pred) cov = y_PCA.inverse_transform(cov) else: y_pred_full = y_pred # Plot regions_all = ['Global'] + list(RegionsList) PlotMap(y_test, y_pred_full, lons, lats, names_train, names_test, plot_dir, area_flat) Validation(y_test, y_pred_full, cov, lons, lats, lons1, lats1, names_train, names_test, plot_dir + TestName[0] + '_', area_flat, metric_regions=regions_all) Save(names_train, names_test, X_train, X_test, y_train, y_test, y_pred_full, lons, lats, lons1, lats1, area_flat, m, kern, plot_dir + TestName[0] + '_')
def TrainTestScale(X, y, Names, TestName, lons, lats, lons1, lats1, area_flat, plot_dir): (X_train, X_test, y_train, y_test, names_train, names_test) = split_set(X, y, Names, TestName) # Scale scaler_x = StandardScaler() scaler_x.fit(X_train) X_train_norm = scaler_x.transform(X_train) X_test_norm = scaler_x.transform(X_test) scaler_y = StandardScaler() scaler_y.fit(y_train) y_train_norm = scaler_y.transform(y_train) y_test_norm = scaler_y.transform(y_test) # Train (m, kern) = TrainModel(X_train_norm, y_train_norm) # Predict y_pred_norm, cov = TestModel(m, X_test_norm, y_test_norm) y_pred = scaler_y.inverse_transform(y_pred_norm) # Plot PlotMap(y_test, y_pred, lons, lats, names_train, names_test, plot_dir, area_flat) regions_all = ['Global'] + list(RegionsList) print(y_pred.shape) for v in range(len(TestName)): Validation(y_test[v:v + 1, ], y_pred[v:v + 1, :], cov[v:v + 1, :], lons, lats, lons1, lats1, names_train, names_test, plot_dir + TestName[v] + '_', area_flat, metric_regions=regions_all) Save(names_train, names_test, X_train, X_test, y_train, y_test, y_pred, lons, lats, lons1, lats1, area_flat, m, kern, plot_dir + TestName[0] + '_')
area_flat = area.flatten() print(area_flat.shape,X_SfcTemp.shape) regions = ['Europe','Africa','US','South_America','East_Asia','India'] # Save to plot_dir = '/work/lm2612/GPPCA/' # Test data set print(Names) for i in range(len(Names)): print("Training Stage:") TestName = Names[i] (X_train,X_test,y_train,y_test,names_train,names_test) = split_set(X,y,Names,TestName) # PCA X_PCA = PCA().fit(X_train) y_PCA = PCA().fit(y_train) X_trans = X_PCA.transform(X_train) y_trans = y_PCA.transform(y_train) # GP Model (N,p) = X_trans.shape kern = GPy.kern.Matern52(p,ARD=True)+GPy.kern.Linear(p,ARD=True) print("kernel used: {}".format(kern)) m = GPy.models.GPRegression(X_trans,y_trans,kern) m.optimize()
def TrainTestRegional(X, y, Names, TestName, lons, lats, lons1, lats1, area_flat, plot_dir, regions=RegionOnlyList, reg_inputs=True, reg_outputs=True): (X_train, X_test, y_train, y_test, names_train, names_test) = split_set(X, y, Names, TestName) # PCA if reg_inputs is True: print(regions) X_trans = split_into_regions(X_train, Area(lons1, lats1).flatten(), lons, lats, lons1, lats1, regions) X_test_trans = split_into_regions(X_test, Area(lons1, lats1).flatten(), lons, lats, lons1, lats1, regions) else: X_trans = X_train X_test_trans = X_test if reg_outputs is True: print(regions) y_trans = split_into_regions(y_train, Area(lons1, lats1).flatten(), lons, lats, lons1, lats1, regions) y_test_trans = split_into_regions(y_test, Area(lons1, lats1).flatten(), lons, lats, lons1, lats1, regions) else: y_trans = y_train y_test_trans = y_test # Train (m, kern) = TrainModel(X_trans, y_trans) y_pred, cov = TestModel(m, X_test_trans, y_test_trans) # Inverse PC if reg_outputs is True: areas = np.ones(len(regions)) RegionalMetrics(y_pred, y_test_trans, regions, lons, lats, lons1, lats1, names_train, names_test, plot_dir + TestName[0] + '_', areas) GPError(cov, y_pred, y_test_trans, regions, lons, lats, lons1, lats1, names_train, names_test, plot_dir + TestName[0] + '_', areas) else: y_pred_full = y_pred # Plot regions_all = ['Global'] + list(RegionsList) print(TestName[0]) PlotMap(y_test, y_pred_full, lons, lats, names_train, names_test, plot_dir, area_flat) Validation(y_test, y_pred_full, cov, lons, lats, lons1, lats1, names_train, names_test, plot_dir + TestName[0] + '_', area_flat, metric_regions=regions_all) Save(names_train, names_test, X_train, X_test, y_train, y_test, y_pred, lons, lats, lons1, lats1, area_flat, m, kern, plot_dir + TestName[0] + '_')