예제 #1
0
    def run(self, max_iter=100):

        candidates = self.generate_descendence(self.initialize_chromosomes(),
                                               self.num_nc)
        self.plotting(candidates)

        best_candidate = candidates[0]
        score = min(candidates[:, -1])
        prev_score = np.Inf

        for iteration in range(0, max_iter):
            if prev_score > score:
                candidates = self.generate_descendence(
                    self.initialize_chromosomes(), self.num_nc)
                self.plotting(candidates)
                prev_score = score
                score = min(candidates[:, -1])
                if prev_score > score:
                    best_candidate = candidates[0]
            else:
                print(
                    'Convergence at iteration {}.\nCandidate Chromosome: {}.\nScore: {}\nCenters at: {}'
                    .format(
                        iteration, best_candidate[0], best_candidate[2],
                        str(''.join(
                            ['\n\t' + str(x) for x in best_candidate[1]]))))
                self.plotting(best_candidate, single_cluster=True)
                break

        w, r = self.get_hyperparameters(best_candidate[0])
        it_best = gk(self.data_scaled, 3, w, r)
        points_closet_centroid = it_best.get_points_closet_centroid(
            best_candidate[1])

        pt.schema(points_closet_centroid, best_candidate[1],
                  points_closet_centroid[:, -1], best_candidate)