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()
示例#2
0
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
示例#3
0
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
示例#4
0
    def run(self):
        self.bisecting = [
            BisectingKmeans(dataset=self.dataset,
                            k=i,
                            trials=self.trials,
                            maxRounds=self.maxRounds) for i in self.krange
        ]
        self.normal = [
            Kmeans(dataset=self.dataset,
                   k=i,
                   trials=self.trials,
                   maxRounds=self.maxRounds) for i in self.krange
        ]

        for i, j in enumerate(self.krange):
            self.bisecting[i].run()
            self.normal[i].run()