Exemple #1
0
    def build_with_ga(self, X, y):

        # accuracy fitness function
        def accuracy_fitness_function(chromosome):
            # decode the class model from gene
            aggregation, mus = _decode(self.m, X, y, self.aggregation_rules__,
                                       self.mu_factories, self.classes_,
                                       chromosome)
            y_pred = _predict(mus, aggregation, self.classes_, X)
            return 1.0 - accuracy_score(y, y_pred)

        # number of genes (2 for the aggregation, 4 for each attribute)
        n_genes = 2 + (self.m * 5 * len(self.classes_))

        logger.info("initializing GA %d iterations" % (self.iterations, ))
        # initialize
        ga = GeneticAlgorithm(
            fitness_function=helper_fitness(accuracy_fitness_function),
            scaling=1.0,
            crossover_function=UniformCrossover(0.5),
            # crossover_points=range(2, n_genes, 5),
            elitism=5,  # no elitism
            n_chromosomes=100,
            n_genes=n_genes,
            p_mutation=0.3,
            random_state=self.random_state)

        last_fitness = None

        #
        iternum = 0
        for generation in range(self.iterations):
            ga.next()
            logger.info("GA iteration %d Fitness (top-4) %s" %
                        (generation, str(np.sort(ga.fitness_)[:4])))
            chromosomes, fitnesses = ga.best(10)
            aggregation, protos = _decode(self.m, X, y,
                                          self.aggregation_rules__,
                                          self.mu_factories, self.classes_,
                                          chromosomes[0])
            self.aggregation = aggregation
            self.protos_ = protos

            # check stopping condition
            new_fitness = np.mean(fitnesses)
            if last_fitness is not None:
                d_fitness = last_fitness - new_fitness
                if self.epsilon is not None and d_fitness < self.epsilon:
                    logger.info("Early stop d_fitness %f" % (d_fitness, ))
                    break
            last_fitness = new_fitness
            iternum = iternum + 1
            print("Iteration = ", iternum, "fitness = ", last_fitness)
        # print learned.
        logger.info("+- Final: Aggregation %s" % (str(self.aggregation), ))
        for key, value in self.protos_.items():
            logger.info("`- Class-%d" % (key, ))
            logger.info("`- Membership-fs %s" %
                        (str([x.__str__() for x in value]), ))
Exemple #2
0
    def build_with_ga(self, X, y):

        # accuracy fitness function
        def accuracy_fitness_function(chromosome):
            # decode the class model from gene
            aggregation, mus = _decode(self.m, X, y, self.aggregation_rules__,
                                       self.mu_factories, self.classes_, chromosome)
            y_pred = _predict(mus, aggregation, self.classes_, X)
            return 1.0 - accuracy_score(y, y_pred)

        # number of genes (2 for the aggregation, 4 for each attribute)
        n_genes = 2 + (self.m * 5 * len(self.classes_))

        logger.info("initializing GA %d iterations" % (self.iterations,))
        # initialize
        ga = GeneticAlgorithm(fitness_function=helper_fitness(accuracy_fitness_function),
                              scaling=1.0,
                              crossover_function=UniformCrossover(0.5),
                              # crossover_points=range(2, n_genes, 5),
                              elitism=5,  # no elitism
                              n_chromosomes=100,
                              n_genes=n_genes,
                              p_mutation=0.3)

        last_fitness = None

        #
        for generation in range(self.iterations):
            ga.next()
            logger.info("GA iteration %d Fitness (top-4) %s" % (generation, str(np.sort(ga.fitness_)[:4])))
            chromosomes, fitnesses = ga.best(10)
            aggregation, protos = _decode(self.m, X, y, self.aggregation_rules__,
                                          self.mu_factories, self.classes_, chromosomes[0])
            self.aggregation = aggregation
            self.protos_ = protos

            # check stopping condition
            new_fitness = np.mean(fitnesses)
            if last_fitness is not None:
                d_fitness = last_fitness - new_fitness
                if self.epsilon is not None and d_fitness < self.epsilon:
                    logger.info("Early stop d_fitness %f" % (d_fitness,))
                    break
            last_fitness = new_fitness

        # print learned.
        logger.info("+- Final: Aggregation %s" % (str(self.aggregation),))
        for key, value in self.protos_.items():
            logger.info("`- Class-%d" % (key,))
            logger.info("`- Membership-fs %s" % (str([ x.__str__() for x in value ]),))
Exemple #3
0
    def build_for_class(self, X, y, class_idx):

        y_target = np.zeros(y.shape)  # create the target of 1 and 0.
        y_target[class_idx] = 1.0

        n_genes = 5 * self.m

        def rmse_fitness_function(chromosome):
            proto = self.decode(chromosome)
            y_pred = _predict_one(proto, self.aggregation, X)
            return mean_squared_error(y_target, y_pred)

        logger.info("initializing GA %d iterations" % (self.iterations, ))
        # initialize
        ga = GeneticAlgorithm(
            fitness_function=helper_fitness(rmse_fitness_function),
            scaling=1.0,
            crossover_function=UniformCrossover(0.5),
            # crossover_points=range(0, n_genes, 5),
            elitism=5,  # no elitism
            n_chromosomes=100,
            n_genes=n_genes,
            p_mutation=0.3)

        # print "population", ga.population_
        # print "fitness", ga.fitness_

        chromosomes, fitnesses = ga.best(10)
        last_fitness = np.mean(fitnesses)

        proto = None
        #
        for generation in range(self.iterations):
            ga.next()
            logger.info("GA iteration %d Fitness (top-4) %s" %
                        (generation, str(ga.fitness_[:4])))
            chromosomes, fitnesses = ga.best(10)
            proto = self.decode(chromosomes[0])

            # check stopping condition
            new_fitness = np.mean(fitnesses)
            d_fitness = last_fitness - new_fitness
            if self.epsilon is not None and d_fitness < self.epsilon:
                logger.info("Early stop d_fitness %f" % (d_fitness, ))
                break
            last_fitness = new_fitness

        return proto
Exemple #4
0
    def build_for_class(self, X, y, class_idx):

        # take column-wise min/mean/max for class
        mins = np.nanmin(X[class_idx], 0)
        means = np.nanmean(X[class_idx], 0)
        maxs = np.nanmax(X[class_idx], 0)
        ds = (maxs - mins) / 2.0

        n_genes = 2 * self.m  # adjustment for r and shrinking/expanding value for p/q

        B = np.ones(n_genes)

        def decode_with_shrinking_expanding(C):
            def dcenter(j):
                return min(1.0, max(0.0,
                                    C[j])) - 0.5 if self.adjust_center else 1.0

            return [
                fl.PiSet(r=means[j] * dcenter(j),
                         p=means[j] - (ds[j] * C[j + 1]),
                         q=means[j] + (ds[j] * C[j + 1]))
                for j in range(self.m)
            ]

        y_target = np.zeros(y.shape)  # create the target of 1 and 0.
        y_target[class_idx] = 1.0

        def rmse_fitness_function(chromosome):
            proto = decode_with_shrinking_expanding(chromosome)
            y_pred = _predict_one(proto, self.aggregation, X)
            return mean_squared_error(y_target, y_pred)

        logger.info("initializing GA %d iterations" % (self.iterations, ))
        # initialize
        ga = UnitIntervalGeneticAlgorithm(
            fitness_function=helper_fitness(rmse_fitness_function),
            crossover_function=UniformCrossover(0.5),
            elitism=3,
            n_chromosomes=100,
            n_genes=n_genes,
            p_mutation=0.3)

        ga = helper_n_generations(ga, self.iterations)
        chromosomes, fitnesses = ga.best(1)

        return decode_with_shrinking_expanding(
            chromosomes[0]), decode_with_shrinking_expanding(B)
Exemple #5
0
    def build_for_class(self, X, y, class_idx):

        y_target = np.zeros(y.shape)  # create the target of 1 and 0.
        y_target[class_idx] = 1.0

        n_genes = 5 * self.m

        def rmse_fitness_function(chromosome):
            proto = self.decode(chromosome)
            y_pred = _predict_one(proto, self.aggregation, X)
            return mean_squared_error(y_target, y_pred)

        logger.info("initializing GA %d iterations" % (self.iterations,))
        # initialize
        ga = GeneticAlgorithm(fitness_function=helper_fitness(rmse_fitness_function),
                              scaling=1.0,
                              crossover_function=UniformCrossover(0.5),
                              # crossover_points=range(0, n_genes, 5),
                              elitism=5,  # no elitism
                              n_chromosomes=100,
                              n_genes=n_genes,
                              p_mutation=0.3)

        # print "population", ga.population_
        # print "fitness", ga.fitness_

        chromosomes, fitnesses = ga.best(10)
        last_fitness = np.mean(fitnesses)

        proto = None
        #
        for generation in range(self.iterations):
            ga.next()
            logger.info("GA iteration %d Fitness (top-4) %s" % (generation, str(ga.fitness_[:4])))
            chromosomes, fitnesses = ga.best(10)
            proto = self.decode(chromosomes[0])

            # check stopping condition
            new_fitness = np.mean(fitnesses)
            d_fitness = last_fitness - new_fitness
            if self.epsilon is not None and d_fitness < self.epsilon:
                logger.info("Early stop d_fitness %f" % (d_fitness,))
                break
            last_fitness = new_fitness

        return proto
Exemple #6
0
    def build_for_class(self, rs, X):

        def distance_fitness(c):
            return np.sum(np.abs(X - c))

        # setup GA
        ga = GeneticAlgorithm(fitness_function=helper_fitness(distance_fitness),
                              elitism=3,
                              n_chromosomes=100,
                              n_genes=X.shape[1],
                              p_mutation=0.3,
                              random_state=rs)

        ga = helper_n_generations(ga, self.n_iterations)  # advance the GA

        # return the best found parameters for this class.
        chromosomes, fitness = ga.best(1)
        return chromosomes[0]
    def build_for_class(self, rs, X):
        def distance_fitness(c):
            return np.sum(np.abs(X - c))

        # setup GA
        ga = GeneticAlgorithm(
            fitness_function=helper_fitness(distance_fitness),
            elitism=3,
            n_chromosomes=100,
            n_genes=X.shape[1],
            p_mutation=0.3,
            random_state=rs)

        ga = helper_n_generations(ga, self.n_iterations)  # advance the GA

        # return the best found parameters for this class.
        chromosomes, fitness = ga.best(1)
        return chromosomes[0]
Exemple #8
0
    def build_for_class(self, X, y, class_idx):

        # take column-wise min/mean/max for class
        mins = np.nanmin(X[class_idx], 0)
        means = np.nanmean(X[class_idx], 0)
        maxs = np.nanmax(X[class_idx], 0)
        ds = (maxs - mins) / 2.0

        n_genes = 2 * self.m  # adjustment for r and shrinking/expanding value for p/q

        B = np.ones(n_genes)

        def decode_with_shrinking_expanding(C):
            def dcenter(j):
                return min(1.0, max(0.0, C[j])) - 0.5 if self.adjust_center else 1.0

            return [ fl.PiSet(r=means[j] * dcenter(j),
                              p=means[j] - (ds[j] * C[j + 1]),
                              q=means[j] + (ds[j] * C[j + 1]))
                     for j in range(self.m) ]

        y_target = np.zeros(y.shape)  # create the target of 1 and 0.
        y_target[class_idx] = 1.0

        def rmse_fitness_function(chromosome):
            proto = decode_with_shrinking_expanding(chromosome)
            y_pred = _predict_one(proto, self.aggregation, X)
            return mean_squared_error(y_target, y_pred)

        logger.info("initializing GA %d iterations" % (self.iterations,))
        # initialize
        ga = UnitIntervalGeneticAlgorithm(fitness_function=helper_fitness(rmse_fitness_function),
                                          crossover_function=UniformCrossover(0.5),
                                          elitism=3,
                                          n_chromosomes=100,
                                          n_genes=n_genes,
                                          p_mutation=0.3)

        ga = helper_n_generations(ga, self.iterations)
        chromosomes, fitnesses = ga.best(1)

        return decode_with_shrinking_expanding(chromosomes[0]), decode_with_shrinking_expanding(B)
Exemple #9
0
    def fit_weights(self, rs, models, X, y):

        n_genes = self.n_models * len(self.classes_)

        def fitness_function(c):
            M = self.predict_(X, models, c)
            y_pred = np.argmin(M, 1)
            return 1.0 - accuracy_score(y, y_pred)

        ga = GeneticAlgorithm(fitness_function=helper_fitness(fitness_function),
                              elitism=3,
                              n_chromosomes=100,
                              n_genes=n_genes,
                              p_mutation=0.3,
                              random_state=rs)

        ga = helper_n_generations(ga, self.n_iterations_weights)  # advance the GA

        chromosomes, fitness = ga.best(1)

        return chromosomes[0]
    def fit_weights(self, rs, models, X, y):

        n_genes = self.n_models * len(self.classes_)

        def fitness_function(c):
            M = self.predict_(X, models, c)
            y_pred = np.argmin(M, 1)
            return 1.0 - accuracy_score(y, y_pred)

        ga = GeneticAlgorithm(
            fitness_function=helper_fitness(fitness_function),
            elitism=3,
            n_chromosomes=100,
            n_genes=n_genes,
            p_mutation=0.3,
            random_state=rs)

        ga = helper_n_generations(ga,
                                  self.n_iterations_weights)  # advance the GA

        chromosomes, fitness = ga.best(1)

        return chromosomes[0]