Skip to content

Instantly share code, notes, and snippets.

@cmd-ntrf
Last active December 14, 2015 06:19
Show Gist options
  • Save cmd-ntrf/5041944 to your computer and use it in GitHub Desktop.
Save cmd-ntrf/5041944 to your computer and use it in GitHub Desktop.
Prototype for a DataLogger object based on pandas DataFrame for the DEAP framework. This object would take care of statistics computation and bookeeping along the evolution.
from collections import defaultdict
from pandas import DataFrame, concat
from sys import stdout
from numpy import random
from matplotlib import pyplot as plt
class DataLogger(object):
def __init__(self, key=None):
self.key = key
self.data = []
self.table = None
self.header = True
def update(self, seq, **kargs):
if self.key is not None:
seq = map(self.key, seq)
stats = DataFrame(seq).describe()
stats = stats.drop('count').T
for i, (name, value) in enumerate(kargs.items()):
stats.insert(i, name, (value,) * len(stats))
self.data.append(stats)
self.table = concat(self.data, keys=range(len(self.data)), names=['gen', 'obj'])
self.ndim = len(stats)
def log(self):
tail = self.table.tail(self.ndim)
string = tail.to_string().split("\n")
if self.header:
print "\n".join(string[0:2])
self.header = False
print "\n".join(string[2:])
if __name__ == "__main__":
logger = DataLogger()
NGEN = 10
SIZE = 20
for i in range(NGEN):
fits = random.randn(SIZE, 2)
logger.update(fits, eval=len(fits))
logger.log()
# We group the results by objective number:
grouped = logger.table.groupby(level=1)
# Then we can simply plot the data
mean_obj1 = grouped.get_group(0)['mean'][:, 0]
mean_obj1.plot()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment