def run(self): worstCluster = self.dataset while len(self.means) < self.k: if isinstance(worstCluster, Dataset): worstDataset = worstCluster elif isinstance(worstCluster, Mean): worstDataset = worstCluster.coveredDataset bisection = Kmeans(dataset=worstDataset, k=2, trials=self.trials, maxRounds=self.trials, key=self.key) bisection.run() bisectionSolution = bisection.getBestSolution() self.means += bisectionSolution.means worstCluster = max(self.means, key=lambda m: m.getMeanSquaredError()) # if the number of means is not enouth remove the worst cluster # found to bisect it in the next iteration. if len(self.means) < self.k: self.means.remove(worstCluster) self.setMeanSquaredError()
def get_colors_from_image(filepath, number_of_colors): # print 'processing '+filepath kmeans = Kmeans(number_of_colors, 6, 5, 200) # default 6,5,200 im = Image.open(filepath) colors = kmeans.run(im) im.close() return colors
def get_color_from_image(filepath): # print 'processing '+filepath kmeans = Kmeans(1) im = Image.open(filepath) color = kmeans.run(im)[0] im.close() return color
def run(self): worstCluster = self.dataset while len(self.means) < self.k: if isinstance(worstCluster, Dataset): worstDataset = worstCluster elif isinstance(worstCluster, Mean): worstDataset = worstCluster.coveredDataset bisection = Kmeans(dataset=worstDataset, k=2, trials=self.trials, maxRounds=self.trials, key=self.key) bisection.run() bisectionSolution = bisection.getBestSolution() self.means += bisectionSolution.means worstCluster = max(self.means, key=lambda m: m.getMeanSquaredError()) # if the number of means is not enouth remove the worst cluster # found to bisect it in the next iteration. if len(self.means) < self.k: self.means.remove(worstCluster) self.setMeanSquaredError()