def RunKPCAShogun(): totalTimer = Timer() try: # Load input dataset. Log.Info("Loading dataset", self.verbose) data = np.genfromtxt(self.dataset, delimiter=',') dataFeat = RealFeatures(data.T) with totalTimer: # Get the new dimensionality, if it is necessary. if "new_dimensionality" in options: d = int(options.pop("new_dimensionality")) if (d > data.shape[1]): Log.Fatal("New dimensionality (" + str(d) + ") cannot be greater " + "than existing dimensionality (" + str(data.shape[1]) + ")!") return -1 else: d = data.shape[1] # Get the kernel type and make sure it is valid. if "kernel" in options: kernel = str(options.pop("kernel")) else: Log.Fatal("Choose kernel type, valid choices are 'linear'," + " 'hyptan', 'polynomial' and 'gaussian'.") return -1 if "degree" in options: degree = int(options.pop("degree")) if len(options) > 0: Log.Fatal("Unknown parameters: " + str(options)) raise Exception("unknown parameters") if kernel == "polynomial": kernel = PolyKernel(dataFeat, dataFeat, degree, True) elif kernel == "gaussian": kernel = GaussianKernel(dataFeat, dataFeat, 2.0) elif kernel == "linear": kernel = LinearKernel(dataFeat, dataFeat) elif kernel == "hyptan": kernel = SigmoidKernel(dataFeat, dataFeat, 2, 1.0, 1.0) else: Log.Fatal("Invalid kernel type (" + kernel.group(1) + "); valid " + "choices are 'linear', 'hyptan', 'polynomial' and 'gaussian'.") return -1 # Perform Kernel Principal Components Analysis. model = KernelPCA(kernel) model.set_target_dim(d) model.init(dataFeat) model.apply_to_feature_matrix(dataFeat) except Exception as e: return -1 return totalTimer.ElapsedTime()
def RunKPCAShogun(q): totalTimer = Timer() try: # Load input dataset. Log.Info("Loading dataset", self.verbose) data = np.genfromtxt(self.dataset, delimiter=',') dataFeat = RealFeatures(data.T) with totalTimer: # Get the new dimensionality, if it is necessary. dimension = re.search('-d (\d+)', options) if not dimension: d = data.shape[1] else: d = int(dimension.group(1)) if (d > data.shape[1]): Log.Fatal("New dimensionality (" + str(d) + ") cannot be greater " + "than existing dimensionality (" + str(data.shape[1]) + ")!") q.put(-1) return -1 # Get the kernel type and make sure it is valid. kernel = re.search("-k ([^\s]+)", options) if not kernel: Log.Fatal("Choose kernel type, valid choices are 'linear'," + " 'hyptan', 'polynomial' and 'gaussian'.") q.put(-1) return -1 elif kernel.group(1) == "polynomial": degree = re.search('-D (\d+)', options) degree = 1 if not degree else int(degree.group(1)) kernel = PolyKernel(dataFeat, dataFeat, degree, True) elif kernel.group(1) == "gaussian": kernel = GaussianKernel(dataFeat, dataFeat, 2.0) elif kernel.group(1) == "linear": kernel = LinearKernel(dataFeat, dataFeat) elif kernel.group(1) == "hyptan": kernel = SigmoidKernel(dataFeat, dataFeat, 2, 1.0, 1.0) else: Log.Fatal("Invalid kernel type (" + kernel.group(1) + "); valid " + "choices are 'linear', 'hyptan', 'polynomial' and 'gaussian'.") q.put(-1) return -1 # Perform Kernel Principal Components Analysis. model = KernelPCA(kernel) model.set_target_dim(d) model.init(dataFeat) model.apply_to_feature_matrix(dataFeat) except Exception as e: q.put(-1) return -1 time = totalTimer.ElapsedTime() q.put(time) return time
def preprocessor_kernelpca_modular(data, threshold, width): from modshogun import RealFeatures from modshogun import KernelPCA from modshogun import GaussianKernel features = RealFeatures(data) kernel = GaussianKernel(features, features, width) preprocessor = KernelPCA(kernel) preprocessor.init(features) preprocessor.set_target_dim(2) # X=preprocessor.get_transformation_matrix() X2 = preprocessor.apply_to_feature_matrix(features) lx0 = len(X2) modified_d1 = zeros((lx0, number_of_points_for_circle1)) modified_d2 = zeros((lx0, number_of_points_for_circle2)) modified_d1 = [X2[i][0:number_of_points_for_circle1] for i in range(lx0)] modified_d2 = [ X2[i][number_of_points_for_circle1 : (number_of_points_for_circle1 + number_of_points_for_circle2)] for i in range(lx0) ] p.plot(modified_d1[0][:], modified_d1[1][:], "o", modified_d2[0][:], modified_d2[1][:], "x") p.title("final data") p.show() return features
def preprocessor_kernelpca_modular (data, threshold, width): from modshogun import RealFeatures from modshogun import KernelPCA from modshogun import GaussianKernel features = RealFeatures(data) kernel = GaussianKernel(features,features,width) preprocessor = KernelPCA(kernel) preprocessor.init(features) preprocessor.set_target_dim(2) preprocessor.apply_to_feature_matrix(features) return features
def preprocessor_kernelpca_modular (data, threshold, width): from modshogun import RealFeatures from modshogun import KernelPCA from modshogun import GaussianKernel features = RealFeatures(data) kernel=GaussianKernel(features,features,width) preprocessor=KernelPCA(kernel) preprocessor.init(features) preprocessor.set_target_dim(2) #X=preprocessor.get_transformation_matrix() X2=preprocessor.apply_to_feature_matrix(features) lx0=len(X2) modified_d1=zeros((lx0,number_of_points_for_circle1)) modified_d2=zeros((lx0,number_of_points_for_circle2)) modified_d1=[X2[i][0:number_of_points_for_circle1] for i in range(lx0)] modified_d2=[X2[i][number_of_points_for_circle1:(number_of_points_for_circle1+number_of_points_for_circle2)] for i in range(lx0)] p.plot(modified_d1[0][:],modified_d1[1][:],'o',modified_d2[0][:],modified_d2[1][:],'x') p.title('final data') p.show() return features
def RunKPCAShogun(q): totalTimer = Timer() try: # Load input dataset. Log.Info("Loading dataset", self.verbose) data = np.genfromtxt(self.dataset, delimiter=',') dataFeat = RealFeatures(data.T) with totalTimer: # Get the new dimensionality, if it is necessary. dimension = re.search('-d (\d+)', options) if not dimension: d = data.shape[1] else: d = int(dimension.group(1)) if (d > data.shape[1]): Log.Fatal("New dimensionality (" + str(d) + ") cannot be greater " + "than existing dimensionality (" + str(data.shape[1]) + ")!") q.put(-1) return -1 # Get the kernel type and make sure it is valid. kernel = re.search("-k ([^\s]+)", options) if not kernel: Log.Fatal( "Choose kernel type, valid choices are 'linear'," + " 'hyptan', 'polynomial' and 'gaussian'.") q.put(-1) return -1 elif kernel.group(1) == "polynomial": degree = re.search('-D (\d+)', options) degree = 1 if not degree else int(degree.group(1)) kernel = PolyKernel(dataFeat, dataFeat, degree, True) elif kernel.group(1) == "gaussian": kernel = GaussianKernel(dataFeat, dataFeat, 2.0) elif kernel.group(1) == "linear": kernel = LinearKernel(dataFeat, dataFeat) elif kernel.group(1) == "hyptan": kernel = SigmoidKernel(dataFeat, dataFeat, 2, 1.0, 1.0) else: Log.Fatal( "Invalid kernel type (" + kernel.group(1) + "); valid " + "choices are 'linear', 'hyptan', 'polynomial' and 'gaussian'." ) q.put(-1) return -1 # Perform Kernel Principal Components Analysis. model = KernelPCA(kernel) model.set_target_dim(d) model.init(dataFeat) model.apply_to_feature_matrix(dataFeat) except Exception as e: q.put(-1) return -1 time = totalTimer.ElapsedTime() q.put(time) return time