Пример #1
0
    def run(self, X, y):
        t = 0
        population = self._generate_initial_population()
        fitness = self.fitness_func(X, y, population)
        past_mean = np.mean(fitness)
        diff_counter = 0

        while t < self.max_ittr and diff_counter < self.lag:
            t += 1
            offspring_population = []

            for _ in range(self.num_exec):
                choice = random()

                parent_a, parent_b = self.selection(population)
                if choice < self.theta_r:
                    child, _ = self.crossover(parent_a, parent_b)
                elif choice < self.theta_r + self.theta_u:
                    child = self.mutation(parent_a)
                else:
                    child = parent_a.copy()

                offspring_population.append(child)

            offspring_fitness = self.fitness_func(X, y, offspring_population)
            temp_fitness = [*fitness, *offspring_fitness]
            temp_population = [*population, *offspring_population]
            best_elements = argsort(temp_fitness)[: self.population_size]

            population = [temp_population[i] for i in best_elements]
            fitness = [temp_fitness[i] for i in best_elements]
            fitness_mean = np.mean(fitness)

            if fitness_mean == past_mean:
                diff_counter += 1
            else:
                diff_counter = 0
                past_mean = fitness_mean

            print(
                "t:",
                t,
                "|| fitness:",
                past_mean,
                "|| std:",
                np.std(fitness),
                "|| dc:",
                diff_counter,
            )

        return population, t
Пример #2
0
    def get_best_tuples(self, X, y, mapping):
        X_train, X_val, y_train, y_val = train_test_split(
            X, y, test_size=self.validation_size, stratify=y)
        o_values = self.get_o_values(
            WiSARD(mapping).fit(X_train,
                                y_train).guarisa_measures(X_val, y_val))
        threshold = self.calculate_threshold(o_values)
        valid_tuples = []
        indexes = argsort(o_values)
        for i in indexes:
            if o_values[i] > threshold:
                valid_tuples.append(mapping[i])

        return valid_tuples, o_values
Пример #3
0
    def get_best_tuples(self, X, y, y_true, mapping):
        X_train, X_val, y_train, y_val = train_test_split(
            X, y, test_size=self.validation_size, stratify=y)
        splitted = get_images_by_label(X_val, y_val)
        o_values = self.get_o_values(
            WiSARD(mapping).fit(X_train, y_train).azhar_measures(
                splitted[y_true], [y_true] * len(splitted[y_true])))
        threshold = self.calculate_threshold(o_values)
        valid_tuples = []
        indexes = argsort(o_values)
        for i in indexes:
            if o_values[i] > threshold:
                valid_tuples.append(mapping[i])

        return valid_tuples, o_values
Пример #4
0
    def get_best_tuples(self, X, y, y_true, particles):
        mapping = self.particles_to_mapping(particles)
        X_train, X_val, y_train, y_val = train_test_split(
            X, y, test_size=self.validation_size, stratify=y)
        splitted = get_images_by_label(X_val, y_val)
        o_values = self.get_o_values(
            WiSARD(mapping).fit(X_train, y_train).azhar_measures(
                splitted[y_true], [y_true] * len(splitted[y_true])))
        threshold = self.calculate_threshold(o_values)
        valid_tuples = []
        indexes = argsort(o_values)
        for i in indexes:
            particles[i].update_best_score(o_values[i])
            if o_values[i] > threshold:
                valid_tuples.append(mapping[i])

        return particles[indexes[0]].get_position(), valid_tuples, particles
Пример #5
0
    def _calculate_score(self, ds, tuple_size):
        with Timer(factor=self.factor) as creation:
            mapping = np.reshape(argsort(self._metric_image), (-1, tuple_size))
            wsd = WiSARD(mapping)

        with Timer(factor=self.factor) as training:
            wsd.fit(ds.X_train, ds.y_train)

        with Timer(factor=self.factor) as classification:
            score = wsd.score(ds.X_test, ds.y_test)

        return {
            "n": tuple_size,
            "accuracy": score,
            "creation_time": creation.elapsed,
            "training_time": training.elapsed,
            "classification_time": classification.elapsed,
        }
Пример #6
0
    def _calculate_score(self, ds, tuple_size):
        with Timer(factor=self.factor) as creation:
            mapping = {}
            for label, img in self._metric_images.items():
                mapping[str(label)] = np.reshape(argsort(img),
                                                 (-1, tuple_size))

            wsd = wp.Wisard(tuple_size, mapping=mapping)

        with Timer(factor=self.factor) as training:
            wsd.train(ds.train)

        with Timer(factor=self.factor) as classification:
            score = wsd.score(ds.test)

        return {
            "n": tuple_size,
            "accuracy": score,
            "creation_time": creation.elapsed,
            "training_time": training.elapsed,
            "classification_time": classification.elapsed,
        }
Пример #7
0
def choice_priorities_to_priority_of_choice(choice_priorities):
    return argsort(choice_priorities, True, True)