def fit_gmm(self, samples): """ Runs a couple of em instances on random starting points and returns internal GMM representation of best instance """ features = RealFeatures(samples.T) gmms = [] log_likelihoods = zeros(self.num_runs_em) for i in range(self.num_runs_em): # set up Shogun's GMM class and run em (corresponds to random # initialisation) gmm = GMM(self.num_components) gmm.set_features(features) log_likelihoods[i] = gmm.train_em() gmms.append(gmm) max_idx = log_likelihoods.argmax() # construct Gaussian mixture components in internal representation components = [] for i in range(self.num_components): mu = gmms[max_idx].get_nth_mean(i) Sigma = gmms[max_idx].get_nth_cov(i) components.append(Gaussian(mu, Sigma)) # construct a Gaussian mixture model based on the best EM run pie = gmms[max_idx].get_coef() proposal = MixtureDistribution(components[0].dimension, self.num_components, components, Discrete(pie)) return proposal
def RunGMMShogun(q): totalTimer = Timer() try: # Load input dataset. Log.Info("Loading dataset", self.verbose) dataPoints = np.genfromtxt(self.dataset, delimiter=',') dataFeat = RealFeatures(dataPoints.T) # Get all the parameters. g = re.search("-g (\d+)", options) n = re.search("-n (\d+)", options) s = re.search("-n (\d+)", options) g = 1 if not g else int(g.group(1)) n = 250 if not n else int(n.group(1)) # Create the Gaussian Mixture Model. model = SGMM(g) model.set_features(dataFeat) with totalTimer: model.train_em(1e-9, n, 1e-9) except Exception as e: q.put(-1) return -1 time = totalTimer.ElapsedTime() q.put(time) return time
def RunGMMShogun(): totalTimer = Timer() try: # Load input dataset. Log.Info("Loading dataset", self.verbose) dataPoints = np.genfromtxt(self.dataset, delimiter=',') dataFeat = RealFeatures(dataPoints.T) # Get all the parameters. if "gaussians" in options: g = int(options.pop("gaussians")) else: Log.Fatal("Required parameter 'gaussians' not specified!") raise Exception("missing parameter") if "max_iterations" in options: n = int(options.pop("max_iterations")) else: n = 0 if len(options) > 0: Log.Fatal("Unknown parameters: " + str(options)) raise Exception("unknown parameters") # Create the Gaussian Mixture Model. model = SGMM(g) model.set_features(dataFeat) with totalTimer: model.train_em(1e-9, n, 1e-9) except Exception as e: Log.Info("Exception: " + str(e)) return -1 return totalTimer.ElapsedTime()