def TrainRBFKernelServer(self, lmbda=.1, gamma=50., sampling_factor=1./1000.): """ Fit Kernelized RBF Ridge Regression to the current image on the server :param lmbda: :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 :return: """ self._BuildDataSet(sampling_factor, normalize=True) inputs = self.inputs outputs = self.outputs labels_input = self.bands_modeling_estimation_input labels_output = self.bands_modeling_estimation_output # Train self.distance_krr = kernel.RBFDistance(kernel.RBFDistance(gamma)) self.gamma = gamma self.lmbda = lmbda self.kernel_rbf = kernel.Kernel(inputs, labels_input, self.distance_krr, weight_property="weight") outputs_eeArray = converters.eeFeatureCollectionToeeArray(outputs, labels_output) self.alpha = self.kernel_rbf.getAlphaeeArray(outputs_eeArray, lmbda)
def TrainLinearServer(self, lmbda=.1, sampling_factor=1. / 100.): """ Fit linear ridge regression to the image :param lmbda: :param sampling_factor: :return: forecasted image :rtype ee.Image """ self._BuildDataSet(sampling_factor, normalize=True) inputs = self.inputs outputs = self.outputs labels_input = self.bands_modeling_estimation_input labels_output = self.bands_modeling_estimation_output # Train outputs_eeArray = converters.eeFeatureCollectionToeeArray( outputs, labels_output) inputs_eeArray = converters.eeFeatureCollectionToeeArray( inputs, labels_input) weight_eeArray = converters.eeFeatureCollectionToeeArray( inputs, ["weight"]) weight_matrix = weight_eeArray.matrixToDiag() inputs_weigted = inputs_eeArray.matrixTranspose().matrixMultiply( weight_matrix) tikhonov = ee.Array.identity( len(labels_input)).multiply(lmbda).multiply( ee.Number(inputs.size())) omega = inputs_weigted.matrixMultiply(inputs_eeArray).add(tikhonov)\ .matrixSolve(inputs_weigted.matrixMultiply(outputs_eeArray)) array_sd = self.inputs_std.pow(-1) array_sd = ee.Array.cat([array_sd], 1).matrixToDiag() self.omega = array_sd.matrixMultiply(omega) prediction_mean = self.inputs_mean.matrixMultiply(self.omega) self.intercept = self.outputs_mean.subtract(prediction_mean) return
def __init__(self, feature_collection, properties, distancia=RBFDistance(.5), weight_property=None): assert type(properties) is list, \ "properties should be a python list object" self.num_rows = ee.Number(feature_collection.size()) # Remove weight propery if present if weight_property is not None: properties = list( filter(lambda prop: prop != weight_property, properties)) self.weight_array = converters.eeFeatureCollectionToeeArray( feature_collection, [weight_property]) else: self.weight_array = ee.Array(ee.List.repeat(1, self.num_rows)) self.weight_array = ee.Array.cat([self.weight_array], 1) self.properties = properties assert len(self.properties) > 1, \ "There is no properties in the current collection" # Get rid of extra columns feature_collection = feature_collection.select(properties) self.feature_collection = feature_collection self.kernel_numpy = None # We store in self.distancia the object which implement the distance self.distancia = distancia self.list_collection = feature_collection.toList(self.num_rows)
def geteeArray(self): return converters.eeFeatureCollectionToeeArray(self.feature_collection, self.properties)