<commit_before>import sys, os
sys.path.append(os.environ['HOME'] + '/git/axe/axe')
sys.path.insert(0, os.getcwd() + '/_imports');
from demos import *
import sk;  # @UnresolvedImport
from dectree import *
from settings import *
from settingsWhere  import *
from pdb import set_trace
from abcd import _Abcd
from Prediction import rforest, CART, Bugs
from methods1 import createTbl
from random import uniform as rand, randint as randi, choice as any
tree = treeings()
# set_trace()

def say(l):
  sys.stdout.write(str(l))

def settings(**d): return o(
  name = "Differention Evolution",
  what = "DE tuner. Tune the planner parameters.",
  author = "<NAME>",
  adaptation = "https://github.com/ai-se/Rahul/blob/master/DEADANT/deadant.py",
  copyleft = "(c) 2014, MIT license, http://goo.gl/3UYBp",
  seed = 1,
  np = 10,
  k = 100,
  tiny = 0.01,
  de = o(np = 5,
       iter = 5,
       epsilon = 1.01,
       N = 20,
       f = 0.3,
       cf = 0.4,
       lives = 100)
  ).update(**d)

The = settings()

class diffEvol(object):
  """
  Differential Evolution
  """

  def __init__(self, model, data):
    self.frontier = []
    self.model = model(data)

  def new(self):
    # Creates a new random instance
    return [randi(d[0], d[1]) for d in self.model.indep()]

  def initFront(self, N):
    # Initialize frontier
    for _ in xrange(N):
      self.frontier.append(self.new())

  def extrapolate(self, l2, l3, l4):
    return [max(d[0],
                min(d[1], int(a + The.de.f * (b - c)))) for a,
                b, c, d in zip(l2, l3, l4, self.model.indep())]

  def one234(self, one, pop, f = lambda x:id(x)):
    def oneOther():
      x = any(pop)
      while f(x) in seen:
        x = any(pop)
      seen.append(f(x))
      return x
    seen = [ f(one) ]
    return oneOther(), oneOther(), oneOther()

  def dominates(self, one, two):
#     set_trace()
    return self.model.depen(one) > self.model.depen(two)

  def DE(self):
    self.initFront(The.de.N)
    lives = The.de.lives
    while lives > 0:
      better = False
      for pos, l1 in enumerate(self.frontier):
        lives -= 1
        l2, l3, l4 = self.one234(l1, self.frontier)
        new = self.extrapolate(l2, l3, l4)
        if  self.dominates(new, l1):
          self.frontier.pop(pos)
          self.frontier.insert(pos, new)
          better = True
        elif self.dominates(l1, new):
          better = False
        else:
          self.frontier.append(new)
          better = True
      if better:
        lives += 1
    return self.frontier


class tuneRF(object):
  # Tune RF
  def __init__(self, data):
    self.data = data
    self.train = createTbl(data[:-1])
    self.test = createTbl([data[-1]])
#   set_trace()
  def depen(self, rows):
    mod = rforest(self.train, self.test
                , tunings = rows  # n_est, max_feat, mss, msl
                , smoteit = True)
    g = _Abcd(before = Bugs(self.test), after = mod, show = False)[-1]
    return g

  def indep(self):
    return [(10, 1e3)  # n_estimators
          , (1, 100)  # max_features
          , (1, 10)  # min_samples_leaf
          , (2, 10)  # min_samples_split
          ]

class tuneWhere2(object):
  # Tune where
  def __init__(self, data):
    self.train = data[:-1]
    self.test = data[-1]

  def depen(self, rows):
    # My where2pred() takes data in string format. Ex: '../Data/ant/ant-1.6.csv'
    actual = Bugs(createTbl(self.test, isBin = True))
    preds = where2prd(self.train, self.test, tunings = rows)
    return _Abcd(before = actual, after = preds, show = True)[-1]

mod = where2pred(self.train, self.test, 
                     tunings = rows,
    
  def indep(self):
    return [(0,1)          # Threshold
          , (0,1)          # InfoGain
          , (1,10)         # Min Sample Size
          , (0,1)          # Min Size
          , (1,6)          # Depth Min
          , (1,20)         # Depth Max
          , (True, False)  # Where Prune?
          , (True, False)] # Tree Prune?
<commit_msg>Issue #11<commit_after>import sys, os
sys.path.append(os.environ['HOME'] + '/git/axe/axe')
sys.path.insert(0, os.getcwd() + '/_imports');
from demos import *
import sk;  # @UnresolvedImport
from dectree import *
from settings import *
from settingsWhere  import *
from pdb import set_trace
from abcd import _Abcd
from Prediction import rforest, CART, Bugs
from methods1 import createTbl
from random import uniform as rand, randint as randi, choice as any
tree = treeings()
# set_trace()

def say(l):
  sys.stdout.write(str(l))

def settings(**d): return o(
  name = "Differention Evolution",
  what = "DE tuner. Tune the planner parameters.",
  author = "<NAME>",
  adaptation = "https://github.com/ai-se/Rahul/blob/master/DEADANT/deadant.py",
  copyleft = "(c) 2014, MIT license, http://goo.gl/3UYBp",
  seed = 1,
  np = 10,
  k = 100,
  tiny = 0.01,
  de = o(np = 5,
       iter = 5,
       epsilon = 1.01,
       N = 20,
       f = 0.3,
       cf = 0.4,
       lives = 100)
  ).update(**d)

The = settings()

class diffEvol(object):
  """
  Differential Evolution
  """

  def __init__(self, model, data):
    self.frontier = []
    self.model = model(data)

  def new(self):
    # Creates a new random instance
    return [randi(d[0], d[1]) for d in self.model.indep()]

  def initFront(self, N):
    # Initialize frontier
    for _ in xrange(N):
      self.frontier.append(self.new())

  def extrapolate(self, l2, l3, l4):
    return [max(d[0],
                min(d[1], int(a + The.de.f * (b - c)))) for a,
                b, c, d in zip(l2, l3, l4, self.model.indep())]

  def one234(self, one, pop, f = lambda x:id(x)):
    def oneOther():
      x = any(pop)
      while f(x) in seen:
        x = any(pop)
      seen.append(f(x))
      return x
    seen = [ f(one) ]
    return oneOther(), oneOther(), oneOther()

  def dominates(self, one, two):
#     set_trace()
    return self.model.depen(one) > self.model.depen(two)

  def DE(self):
    self.initFront(The.de.N)
    lives = The.de.lives
    while lives > 0:
      better = False
      for pos, l1 in enumerate(self.frontier):
        lives -= 1
        l2, l3, l4 = self.one234(l1, self.frontier)
        new = self.extrapolate(l2, l3, l4)
        if  self.dominates(new, l1):
          self.frontier.pop(pos)
          self.frontier.insert(pos, new)
          better = True
        elif self.dominates(l1, new):
          better = False
        else:
          self.frontier.append(new)
          better = True
      if better:
        lives += 1
    return self.frontier


class tuneRF(object):
  # Tune RF
  def __init__(self, data):
    self.data = data
    self.train = createTbl(data[:-1])
    self.test = createTbl([data[-1]])
#   set_trace()
  def depen(self, rows):
    mod = rforest(self.train, self.test
                , tunings = rows  # n_est, max_feat, mss, msl
                , smoteit = True)
    g = _Abcd(before = Bugs(self.test), after = mod, show = False)[-1]
    return g

  def indep(self):
    return [(10, 1e3)  # n_estimators
          , (1, 100)  # max_features
          , (1, 10)  # min_samples_leaf
          , (2, 10)  # min_samples_split
          ]

class tuneWhere2(object):
  # Tune where
  def __init__(self, data):
    self.train = data[:-1]
    self.test = data[-1]
    self.tree = treeings()

  def depen(self, rows):
    # My where2pred() takes data in string format. Ex: '../Data/ant/ant-1.6.csv'
    
    actual = Bugs(createTbl(self.test, isBin = True))
    preds = where2prd(self.train, self.test, tunings = rows)
    return _Abcd(before = actual, after = preds, show = True)[-1]

  def indep(self):
    return [(0,1)          # Threshold
          , (0,1)          # InfoGain
          , (1,10)         # Min Sample Size
          , (0,1)          # Min Size
          , (1,6)          # Depth Min
          , (1,20)         # Depth Max
          , (True, False)  # Where Prune?
          , (True, False)] # Tree Prune?

{'fraction_non_alphanumeric': 0.14264055590650662, 'fraction_numerical': 0.02514213518635502, 'mean_word_length': 2.188078936770036, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1148589', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2845, 'n_words': 742}