def TrainRBFKernelLocal(self, lmbda=.1, gamma=.5, sampling_factor=1./100., with_task=False, with_cross_validation=False): """ Fit Kernelized RBF Ridge Regression to the current image subsampled. It downloads the data to fit. It uses sklearn.kernel_ridge library. :param lmbda: regularization factor :param gamma: gamma factor to build the kernel (https://en.wikipedia.org/wiki/Radial_basis_function_kernel) :param sampling_factor: percentage of pixels to sample to build the model :param with_cross_validation: donwload the data to fit the model with a task :param with_task: donwload the data to fit the model with a task :return: """ best_params = None if not with_cross_validation: best_params = {"alpha": lmbda, "gamma":gamma} modelo = model_sklearn.KRRModel(best_params=best_params,verbose=1) self._BuildDataSet(sampling_factor, normalize=False) ds_total = converters.eeFeatureCollectionToPandas(self.datos, self.bands_modeling_estimation+["weight"], with_task=with_task) logger.info("Size of downloaded ds: {}".format(ds_total.shape)) output_mean,output_std = model_sklearn.fit_model_local(ds_total,modelo, self.bands_modeling_estimation_input, self.bands_modeling_estimation_output) if with_cross_validation: logger.info("Best params: {}".format(modelo.named_steps["randomizedsearchcv"].best_params_)) ridge_kernel = modelo.named_steps["randomizedsearchcv"].best_estimator_ else: ridge_kernel = modelo.named_steps["kernelridge"] # Copy model parameters self.gamma = ridge_kernel.gamma self.lmbda = ridge_kernel.alpha self.alpha = ee.Array(ridge_kernel.dual_coef_.tolist()) # column vector self.inputs_krr =ee.Array(ridge_kernel.X_fit_.tolist()) self.distance_krr = kernel.RBFDistance(self.gamma) self.kernel_rbf = kernel.Kernel(self.inputs, self.bands_modeling_estimation_input, self.distance_krr, weight_property="weight") # Copy normalization stuff self.inputs_mean = ee.Array(modelo.named_steps["standardscaler"].mean_.tolist()) self.inputs_std = ee.Array(modelo.named_steps["standardscaler"].scale_.tolist()) self.outputs_mean = ee.Array(output_mean.tolist()) self.outputs_std = ee.Array(output_std.tolist()) return
def TrainLinearLocal(self, lmbda=.1, sampling_factor=1. / 100., with_task=False, divide_lmbda_by_n_samples=False, with_cross_validation=False): """ Fit linear ridge regression to the image downloading the data and doing the fitting with sklearn.linear_model. :param lmbda: :param sampling_factor: :param divide_lmbda_by_n_samples: :param with_task: if the download is done using a task (require pydrive) :param with_cross_validation: donwload the data to fit the model with a task :return: forecasted image :rtype ee.Image """ from ee_ipl_uv import model_sklearn self._BuildDataSet(sampling_factor, normalize=False) ds_download_pd = converters.eeFeatureCollectionToPandas( self.datos, self.bands_modeling_estimation + ["weight"], with_task=with_task) if divide_lmbda_by_n_samples: lmbda /= ds_download_pd.shape[0] logger.info("Size of downloaded ds: {}".format(ds_download_pd.shape)) best_params = None if not with_cross_validation: best_params = {"alpha": lmbda} model = model_sklearn.LinearModel(best_params=best_params) output_mean, output_std = model_sklearn.fit_model_local( ds_download_pd, model, self.bands_modeling_estimation_input, self.bands_modeling_estimation_output) if with_cross_validation: logger.info("Best params: {}".format(model.best_params_)) model = model.best_estimator_ self.lmbda = model.alpha omega = model.coef_.T * output_std intercept = model.intercept_ * output_std + output_mean self.omega = ee.Array(omega.tolist()) self.intercept = ee.Array([intercept.tolist()]) return