def __kernel_regress(fn, X, y, x): if fn == 0: kernel = C(1.0, (1e-3, 1e3)) * RBF(1, (1e-2, 1e2)) elif fn == 1: kernel = C(1.0, (1e-3, 1e3)) * RQ(length_scale=5.0, alpha=5.0, length_scale_bounds=(1e-02, 1e2), alpha_bounds=(1e-02, 1e2)) gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9) gp.fit(X, y) y_pred, sigma = gp.predict(x, return_std=True) return y_pred[:,0], sigma
def opt_gp(self): self.nrst = 10 self.kernel = RQ(length_scale=1.0, alpha=0.1, length_scale_bounds=(1e-02, 1e2), alpha_bounds=(1e-2, 1e2)) self.gp = GaussianProcessRegressor(kernel=self.kernel, n_restarts_optimizer=self.nrst) return
def get_gpr(kernel_type, hyp, nrst=10, trw=27): if kernel_type == "RBF": kernel = RBF(length_scale=hyp["l"], length_scale_bounds=(1e-02, 1e2)) if kernel_type == "RQ": kernel = RQ(length_scale=hyp["l"], alpha=hyp["a"], length_scale_bounds=(1e-02, 1e2), alpha_bounds=(1e-2, 1e2)) if kernel_type == "Matern": kernel = Matern(length_scale=hyp["l"], length_scale_bounds=(1e-02, 1e2), nu=1.4) gpr = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=nrst) return (gpr, "GPR", trw)
def ini(self): self.nrst = 10 if self.k_type == "RBF": self.kernel = RBF(length_scale=1.0, length_scale_bounds=(1e-02, 1e2)) if self.k_type == "RQ": self.kernel = RQ(length_scale=1.0, alpha=0.1, length_scale_bounds=(1e-02, 1e2), alpha_bounds=(1e-2, 1e2)) if self.k_type == "Matern": self.kernel = Matern(length_scale=1.0, length_scale_bounds=(1e-02, 1e2), nu=1.4) return
def constructModel(self): optimizedHyperParameters = self.optimizedHyperParameters fixedHyperParameters = self.fixedHyperParameters kernelName = optimizedHyperParameters["kernelName"] if kernelName == 'RBF': kernel = C(1.0, (1e-5, 1e5)) * RBF(1.0, (1e-5, 1e5)) elif kernelName == 'Matern': kernel = M(1.0, (1e-5, 1e5), nu=1.5) elif kernelName == 'RationalQuadratic': kernel = RQ(1.0, 1.0, (1e-5, 1e5), (1e-5, 1e5)) model = GaussianProcessRegressor(kernel=kernel) self.model = model
while (n_out > 0 or final_fit==0) and num_finite >= 2: beta1, beta0, incert_slope, _, _ = ft.wls_matrix(time_vals[good_vals], data_vals[good_vals], 1. / err_vals[good_vals], conf_slope=0.99) # standardized dispersion from linearity res_stdized = np.sqrt(np.mean( (data_vals[good_vals] - (beta0 + beta1 * time_vals[good_vals])) ** 2 / err_vals[good_vals])) res = np.sqrt(np.mean((data_vals[good_vals] - (beta0 + beta1 * time_vals[good_vals])) ** 2)) if perc_nonlin[min(niter, len(perc_nonlin) - 1)] == 0: opt_var = 0 else: opt_var = (res / res_stdized ** 2) ** 2 * 100. / (5 * perc_nonlin[min(niter, len(perc_nonlin) - 1)]) k1 = PairwiseKernel(1, metric='linear') + PairwiseKernel(1, metric='linear') * C(opt_var) * RQ(10, 3) # linear kernel k2 = C(30) * ESS(length_scale=1, periodicity=1) # periodic kernel k3 = C(50) * RBF(0.75) kernel = k1 + k2 + k3 mu_x = np.nanmean(time_vals[good_vals]) detr_t_pred = t_pred - mu_x detr_time_vals = time_vals - mu_x mu_y = np.nanmean(data_vals) detr_data_vals = data_vals - mu_y # if we remove a linear trend, normalize_y should be false... gp = GaussianProcessRegressor(kernel=kernel, optimizer=optimizer, n_restarts_optimizer=n_restarts_optimizer, alpha=err_vals[good_vals], normalize_y=False) gp.fit(detr_time_vals[good_vals].reshape(-1, 1), detr_data_vals[good_vals].reshape(-1, 1)) y_pred, sigma = gp.predict(detr_t_pred.reshape(-1, 1), return_std=True)
def fit_tile(tile, tmp_ref_dir, base_dir, out_dir): method = 'gpr' subspat = None ref_dem_date = np.datetime64('2013-01-01') gla_mask = '/calcul/santo/hugonnet/outlines/rgi60_merge.shp' inc_mask = '/calcul/santo/hugonnet/outlines/rgi60_buff_10.shp' write_filt = True clobber = True tstep = 1. / 12. time_filt_thresh = [-50, 50] opt_gpr = False filt_ref = 'both' filt_ls = False conf_filt_ls = 0.99 # specify the exact temporal extent needed to be able to merge neighbouring stacks properly tlim = [np.datetime64('2000-01-01'), np.datetime64('2020-01-01')] #for sensitivity test: force final fit only, and change kernel parameters in entry of script force_final_fit = True k1 = PairwiseKernel(1, metric='linear') # linear kernel k2 = C(period_var) * ESS(length_scale=1, periodicity=1) # periodic kernel k3 = C(base_var * 0.6) * RBF(base_length * 0.75) + C( base_var * 0.3) * RBF(base_length * 1.5) + C(base_var * 0.1) * RBF( base_length * 3) k4 = PairwiseKernel(1, metric='linear') * C(nonlin_var) * RQ( nonlin_length, nonlin_alpha) kernel = k1 + k2 + k3 + k4 lat, lon = SRTMGL1_naming_to_latlon(tile) epsg, utm = latlon_to_UTM(lat, lon) print('Fitting tile: ' + tile + ' in UTM zone ' + utm) # reference DEM ref_utm_dir = os.path.join(tmp_ref_dir, utm) ref_vrt = os.path.join(ref_utm_dir, 'tmp_' + utm + '.vrt') infile = os.path.join(base_dir, utm, tile + '.nc') outfile = os.path.join(out_dir, utm, tile + '_final.nc') fn_filt = os.path.join(base_dir, utm, tile + '_filtered.nc') if True: #not os.path.exists(outfile): ft.fit_stack(infile, fn_filt=fn_filt, fit_extent=subspat, fn_ref_dem=ref_vrt, ref_dem_date=ref_dem_date, exc_mask=gla_mask, tstep=tstep, tlim=tlim, inc_mask=inc_mask, filt_ref=filt_ref, time_filt_thresh=time_filt_thresh, write_filt=True, outfile=outfile, method=method, filt_ls=filt_ls, conf_filt_ls=conf_filt_ls, nproc=nproc, clobber=True, kernel=kernel, force_final_fit=force_final_fit) # write dh/dts for visualisation ds = xr.open_dataset(outfile) t0 = np.datetime64('2000-01-01') t1 = np.datetime64('2020-01-01') ft.get_full_dh(ds, os.path.join( os.path.dirname(outfile), os.path.splitext(os.path.basename(outfile))[0]), t0=t0, t1=t1) else: print('Tile already processed.')
if diff - diff_std >0: base_var = 50 + (diff-diff_std)**2/2 else: base_var = 50. else: base_var = 50. else: nonlin_var = (res / res_stdized) ** 2 # nonlin_var = 1 period_nonlinear = 100. / res_stdized ** 2 # period_nonlinear = 10. base_var=50 print(base_var) k1 = PairwiseKernel(1, metric='linear') + PairwiseKernel(1, metric='linear') * C(nonlin_var) * RQ(10, period_nonlinear) # linear kernel k2 = C(30) * ESS(length_scale=1, periodicity=1) # periodic kernel k3 = C(base_var) * RBF(1.5) kernel = k1 + k2 + k3 mu_x = np.nanmean(time_vals[good_vals]) detr_t_pred = t_pred - mu_x detr_time_vals = time_vals - mu_x mu_y = np.nanmean(data_vals) detr_data_vals = data_vals - mu_y # if we remove a linear trend, normalize_y should be false... gp = GaussianProcessRegressor(kernel=kernel, optimizer=optimizer, n_restarts_optimizer=n_restarts_optimizer, alpha=err_vals[good_vals], normalize_y=False) gp.fit(detr_time_vals[good_vals].reshape(-1, 1), detr_data_vals[good_vals].reshape(-1, 1)) y_pred, sigma = gp.predict(detr_t_pred.reshape(-1, 1), return_std=True)
tmp_x = [data[i][j] for j in features] X.append(tmp_x) Y0.append(data[i][targets[0]]) Y1.append(data[i][targets[1]]) Y2.append(data[i][targets[2]]) Y3.append(data[i][targets[3]]) # finishing pasing data, now start testing # Instanciate a Gaussian Process model #kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2)) #gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10) # TESTIND DIFFERENT KERNELS printMSE = True printDetails = False kernels = [RBF(), MA(), RQ(), RQ(length_scale=50)] for kernel in kernels: print kernel gp = GaussianProcessRegressor(kernel) #n_restarts_optimizer) clf = gp print "variable number 1: " + targets[0] scores = cross_val_score(clf, X, Y0, cv=5, scoring='r2') if printDetails: print scores print "R2: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2) if printMSE: scores = cross_val_score(clf, X, Y0,
xTrain, yTrain = readTrainFile("Data/problem4b_train.csv") xTest = readTestFile("Data/problem4b_test.csv") yTest = readSolutionFile("Data/problem4b_sol.csv") xTrain = np.atleast_2d(xTrain) xTest = np.atleast_2d(xTest) C1 = C(1.0, (1e-3, 1e3)) C2 = C(0.5, (1e-3, 1e3)) RBF1 = RBF(10, (1e-2, 1e2)) RBF2 = RBF(5, (1e-2, 1e2)) RBF3 = RBF(2, (1e-2, 1e2)) RBF4 = RBF(1, (1e-2, 1e2)) RBF5 = RBF(0.5, (1e-2, 1e2)) RQ1 = RQ(10, 0.5, (1e-2, 1e2)) ESS1 = ESS(1.0, 1.0, (1e-05, 100000.0), (1e-05, 100000.0)) kernel1 = C1 * RBF1 + C2 kernel2 = C1 * RBF1 * RBF2 * RBF3 * RBF4 + RBF5 kernel3 = C1 * RQ1 + RBF2 kernel4 = C1 * ESS1 + RBF2 GP = [] for ndx, kernel in zip([1, 2, 3, 4], [kernel1, kernel2, kernel3, kernel4]): t = time.time() print( '---------------------------------------------------------------------------' ) print(datetime.datetime.now()) print(f'time - {t} :: Fitting GP for kernel - {ndx}')
plt.show() # 数据归一化 data = (data - np.min(data)) / (np.max(data) - np.min(data)) ptr, ttr, pte, tte = split_data(data, n) ptr = np.fliplr(ptr) pte = np.fliplr(pte) best_pop = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) p_tr, p_te = translateDNA(best_pop, ptr, pte) # svr clf = svm.SVR() clf.fit(p_tr, ttr) result_svm = clf.predict(p_te) #gpr kernel = RQ(1.0, 1.0, (1e-5, 1e5), (1e-5, 1e5)) reg = GaussianProcessRegressor(kernel=kernel) reg.fit(p_tr, ttr) #这是拟合高斯过程回归的步骤 result_gpr = reg.predict(p_te) #elm Weights, biases, w_out, TF, TYPE = elm.fit(p_tr, ttr) result_elm = elm.predict(p_te, Weights, biases, w_out, TF, TYPE) plt.figure() plt.plot(tte, c='r', label='true') plt.plot(result_svm, c='g', label='svr') plt.plot(result_elm, c='y', label='elm') plt.plot(result_gpr, c='b', label='gpr') plt.legend()
def interp_3d(coord_arr, val_arr, resolution=100j, lattice=None, xlim=(0, 1), ylim=(0, 1), zlim=(0, 1), method='linear', return_grids=False): if lattice is None: min_x, max_x = xlim min_y, max_y = ylim min_z, max_z = zlim else: # get the limits of the lattice along the slice direction x_lat = lattice[:, 0] y_lat = lattice[:, 1] z_lat = lattice[:, 2] # for plotting xlim and ylim min_x, max_x = min(x_lat), max(x_lat) min_y, max_y = min(y_lat), max(y_lat) min_z, max_z = min(z_lat), max(z_lat) x, y, z = coord_arr[:, 0], coord_arr[:, 1], coord_arr[:, 2] points = np.array(list(zip(x, y, z))) # grid on which to evaluate interpolators grid_x, grid_y, grid_z = np.mgrid[min_x:max_x:resolution * 1j, min_y:max_y:resolution * 1j, min_z:max_z:resolution * 1j] if method == 'kriging': uk3d = UniversalKriging3D(x, y, z, val_arr, variogram_model='spherical', nlags=10, enable_plotting=False, drift_terms=['regional_linear'], verbose=False) gridx = np.linspace(min_x, max_x, resolution) gridy = np.linspace(min_y, max_y, resolution) gridz = np.linspace(min_z, max_z, resolution) preds, uk_ss3d = uk3d.execute('grid', gridx, gridy, gridz) preds = preds.data elif method == 'gp': kernel = C(1.0, (1e-3, 1e3)) * RQ(2.0, 1.0, (1e-1, 2e1), (1e-3, 1e3)) gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10, alpha=1e-10) gp.fit(points, val_arr) preds = gp.predict( np.array(list(zip(grid_x.ravel(), grid_y.ravel(), grid_z.ravel())))).reshape( resolution, resolution, resolution) elif method == 'rbf': rbfi = Rbf(x, y, z, val_arr) preds = rbfi(grid_x, grid_y, grid_z) elif method == 'linear': lndi = LND(points, val_arr) preds = lndi(grid_x, grid_y, grid_z) elif method == 'nearest': nndi = NND(points, val_arr) preds = nndi(grid_x, grid_y, grid_z) if return_grids: return preds, grid_x, grid_y, grid_z else: return preds
import matplotlib.pyplot as plt import pandas as pd #Scikit Gaussian Process functions from sklearn.gaussian_process import GaussianProcessRegressor from sklearn.gaussian_process.kernels import PairwiseKernel, RBF, Product, ConstantKernel as C, RationalQuadratic as RQ, Matern, WhiteKernel #Scikit MultiOutput Regression class - not needed in the current version #from sklearn.multioutput import MultiOutputRegressor sn_lc = pd.read_csv("Users/bhagyasubrayan/Desktop/ZTF/ZTF18abbpeqo.r.txt", sep=" ", header = 0, names = ['Date','Band','Magnitude','Mag_Error']) #snlc = sn_lc[['Date','Magnitude','Mag_Error']].to_numpy() #print(snlc) x = sn_lc['Date'][:,np.newaxis] y = sn_lc['Magnitude'][:,np.newaxis] #print(x) #print(y) y_true_stdev = np.std(y) kern = y_true_stdev*y_true_stdev*RQ(length_scale = 10)*RQ(length_scale = 10) gp = GaussianProcessRegressor(kernel=kern).fit(x,y) x_test = (sn_lc['Date'].max() - sn_lc['Date'].min()) * np.random.random_sample((1000,)) + sn_lc['Date'].min() print(x_test) y_mean, y_stdev = gp.predict(x_test[:,np.newaxis], return_std = True) print(y_mean[:,0]) plt.errorbar(x, y, sn_lc['Mag_Error'][:,np.newaxis], fmt='o') plt.gca().invert_yaxis() plt.show() results = np.array([x_test,y_mean[:,0]]) print(results) plt.errorbar(results[0],results[1],y_stdev, fmt='o') plt.gca().invert_yaxis() plt.show()
def constructModel(self): kernel = RQ(1.0, 1.0, (1e-5, 1e5), (1e-5, 1e5)) self.recurrentModel = GaussianProcessRegressor(kernel=kernel)
res_stdized = np.sqrt( np.mean( (data_vals[good_vals] - (beta0 + beta1 * time_vals[good_vals]))**2 / err_vals[good_vals])) res = np.sqrt( np.mean((data_vals[good_vals] - (beta0 + beta1 * time_vals[good_vals]))**2)) if perc_nonlin[min(niter, len(perc_nonlin) - 1)] == 0: opt_var = 0 else: opt_var = (res / res_stdized**2)**2 * 100. / ( 5 * perc_nonlin[min(niter, len(perc_nonlin) - 1)]) k1 = PairwiseKernel(1, metric='linear') + PairwiseKernel( 1, metric='linear') * C(opt_var) * RQ(10, 3) # linear kernel k2 = C(30) * ESS(length_scale=1, periodicity=1) # periodic kernel k3 = C(50) * RBF(0.75) kernel = k1 + k2 + k3 mu_x = np.nanmean(time_vals[good_vals]) detr_t_pred = t_pred - mu_x detr_time_vals = time_vals - mu_x mu_y = np.nanmean(data_vals) detr_data_vals = data_vals - mu_y # if we remove a linear trend, normalize_y should be false... gp = GaussianProcessRegressor(kernel=kernel, optimizer=optimizer, n_restarts_optimizer=n_restarts_optimizer, alpha=err_vals[good_vals],