在 DEAP 中访问多个统计数据
Access multiple statistics in DEAP
在 the DEAP tutorial 之后,我正在尝试使用多个统计信息。
import numpy as np
import random
from deap import base, creator, tools, algorithms
# Define toolbox
toolbox = base.Toolbox()
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
IND_SIZE=5
toolbox.register("individual", tools.initRepeat, creator.Individual, random.random, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
popul = toolbox.population(n=7)
def evaluate(individual):
a = sum(individual)
b = len(individual)
return (a / b,)
toolbox.register('evaluate', evaluate)
toolbox.register('mutate', tools.mutGaussian, mu=0.0, sigma=0.2, indpb=0.1)
toolbox.register('mate', tools.cxUniform, indpb=0.4)
toolbox.register('select', tools.selBest)
# Define statistical tools
stats_fit = tools.Statistics(key=lambda ind: ind.fitness.values)
stats_size = tools.Statistics(key=len)
mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
mstats.register("avg", np.mean)
mstats.register("std", np.std)
# Run algorithm
popul, mlogbook = algorithms.eaSimple(popul, toolbox, cxpb=0.5, mutpb=0.2, ngen=10,
stats=mstats, verbose=True)
这会在屏幕上打印统计数据。
文档指出
The multi-statistics object can be given to an algorithm [...] using
the exact same procedure as the simple statistics.
但是当我尝试访问日志时,它 returns 是 None
的列表。
In: mlogbook.select("avg")
Out: [None, None, None, None, None, None, None, None, None, None, None]
如果我使用常规统计而不是多次统计,我不会遇到这个问题:logbook.select("avg")
returns 一个浮点数列表。
如何在使用多个统计数据时访问记录的统计数据?
需要指定选择统计的章节。例如,
mlogbook.chapters["fitness"].select("avg")
returns跨代适应度平均值的期望列表。
在 the DEAP tutorial 之后,我正在尝试使用多个统计信息。
import numpy as np
import random
from deap import base, creator, tools, algorithms
# Define toolbox
toolbox = base.Toolbox()
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
IND_SIZE=5
toolbox.register("individual", tools.initRepeat, creator.Individual, random.random, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
popul = toolbox.population(n=7)
def evaluate(individual):
a = sum(individual)
b = len(individual)
return (a / b,)
toolbox.register('evaluate', evaluate)
toolbox.register('mutate', tools.mutGaussian, mu=0.0, sigma=0.2, indpb=0.1)
toolbox.register('mate', tools.cxUniform, indpb=0.4)
toolbox.register('select', tools.selBest)
# Define statistical tools
stats_fit = tools.Statistics(key=lambda ind: ind.fitness.values)
stats_size = tools.Statistics(key=len)
mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
mstats.register("avg", np.mean)
mstats.register("std", np.std)
# Run algorithm
popul, mlogbook = algorithms.eaSimple(popul, toolbox, cxpb=0.5, mutpb=0.2, ngen=10,
stats=mstats, verbose=True)
这会在屏幕上打印统计数据。
文档指出
The multi-statistics object can be given to an algorithm [...] using the exact same procedure as the simple statistics.
但是当我尝试访问日志时,它 returns 是 None
的列表。
In: mlogbook.select("avg")
Out: [None, None, None, None, None, None, None, None, None, None, None]
如果我使用常规统计而不是多次统计,我不会遇到这个问题:logbook.select("avg")
returns 一个浮点数列表。
如何在使用多个统计数据时访问记录的统计数据?
需要指定选择统计的章节。例如,
mlogbook.chapters["fitness"].select("avg")
returns跨代适应度平均值的期望列表。