def test_generational_generation(): from EvoDAG.population import Generational from EvoDAG import EvoDAG function_set = [x for x in EvoDAG()._function_set if x.regression] gp = EvoDAG(population_class='Generational', classifier=False, function_set=function_set, popsize=10) gp.X = X y = cl.copy() y[y != 1] = -1 gp.y = y gp.create_population() assert isinstance(gp.population, Generational) p = [] for i in range(gp.popsize - 1): a = gp.random_offspring() p.append(a) gp.replace(a) assert len(gp.population._inner) == (gp.popsize - 1) a = gp.random_offspring() p.append(a) gp.replace(a) assert len(gp.population._inner) == 0 for a, b in zip(gp.population.population, p): assert a == b
def test_model_graphviz(): from EvoDAG import RootGP from EvoDAG.node import Function import tempfile y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp = RootGP(generations=3, tournament_size=2, early_stopping_rounds=-1, classifier=False, pr_variable=1, seed=0, popsize=10).fit(X, y) m = gp.model() print(m._hist) print(m._hist[-1].position, m._hist[-1]._variable) with tempfile.TemporaryFile('w+') as io: m.graphviz(io) io.seek(0) l = io.readlines() cnt = len(m._hist) for k in m._hist: if isinstance(k, Function): v = k._variable if isinstance(v, list): cnt += len(v) else: cnt += 1 print("".join(l)) print(cnt, len(l)) assert 2 + cnt == len(l)
def test_g_precision(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=200, time_limit=0.9, fitness_function='g_precision', multiple_outputs=True, seed=0, popsize=1000) gp.y = y gp.X = X gp.create_population() # off = gp.random_offspring() off = gp.population.bsf hy = SparseArray.argmax(off.hy) index = np.array(gp._mask_ts.index) y = np.array(gp._y_klass.full_array())[index] hy = np.array(hy.full_array())[index] nclasses = gp._bagging_fitness.nclasses precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)]) score = np.prod(precision) - 1 assert gp._fitness_function == 'g_precision' gp._bagging_fitness.set_fitness(off) assert_almost_equals(score, off.fitness) index = np.array(gp._mask_ts.full_array()) == 0 y = np.array(gp._y_klass.full_array())[index] hy = SparseArray.argmax(off.hy) hy = np.array(hy.full_array())[index] precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)]) score = np.prod(precision) - 1 if np.isfinite(score) and np.isfinite(off.fitness_vs): assert_almost_equals(score, off.fitness_vs)
def test_model_hist(): from EvoDAG import EvoDAG from EvoDAG.base import Model y = cl.copy() gp = EvoDAG(generations=np.inf, multiple_outputs=True, tournament_size=2, early_stopping_rounds=-1, seed=1, popsize=30).fit(X[:-10], y[:-10], test_set=X[-10:]) hist = gp.population.hist trace = gp.trace(gp.population.estopping) a = hist[trace[-1]].variable if not isinstance(a, list): a = [a] m = Model(trace, hist) b = m._hist[-1].variable if not isinstance(b, list): b = [b] print([(x, x.height) for x in m._hist]) print((m._map, a, b)) for v1, v2 in zip(a, b): if v1 not in m._map: assert v1 == v2 else: assert m._map[v1] == v2
def test_a_precision(): from EvoDAG.cython_utils import Score from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=0, popsize=500) gp.y = y gp.X = X gp.create_population() # off = gp.random_offspring() off = gp.population.bsf hy = SparseArray.argmax(off.hy) index = np.array(gp._mask_ts.index) y = np.array(gp._y_klass.full_array())[index] hy = np.array(hy.full_array())[index] nclasses = gp._bagging_fitness.nclasses precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)]) f1 = Score(nclasses) mf1, mf1_v = f1.a_precision(gp._y_klass, SparseArray.argmax(off.hy), gp._mask_ts.index) assert_almost_equals(np.mean(precision), mf1) gp._fitness_function = 'a_precision' gp._bagging_fitness.set_fitness(off) assert_almost_equals(mf1 - 1, off.fitness) index = np.array(gp._mask_ts.full_array()) == 0 y = np.array(gp._y_klass.full_array())[index] hy = SparseArray.argmax(off.hy) hy = np.array(hy.full_array())[index] precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)]) assert_almost_equals(np.mean(precision) - 1, off.fitness_vs)
def test_ensemble_model(): from EvoDAG import RootGP from EvoDAG.model import Ensemble y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gps = [ RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, classifier=False, seed=seed, popsize=10).fit(X[:-10], y[:-10], test_set=X) for seed in range(3) ] ens = Ensemble([gp.model() for gp in gps]) res = [gp.decision_function() for gp in gps] res = np.median([x.full_array() for x in res], axis=0) res = SparseArray.fromlist(res) print(res) # res = Add.cumsum(res) * (1 / 3.) r2 = ens.decision_function(None) print(res.full_array()[:10], r2.full_array()[:10]) print(res.SSE(r2)) assert res.SSE(r2) == 0
def test_random_generations(): from EvoDAG import EvoDAG from EvoDAG.population import SteadyState class P(SteadyState): def random_selection(self, negative=False): raise RuntimeError('!') y = cl.copy() y[y != 1] = -1 for pop in ['SteadyState', 'Generational', P]: gp = EvoDAG(population_class=pop, classifier=False, all_inputs=True, random_generations=1, early_stopping_rounds=1, popsize=2) gp.X = X gp.y = y gp.create_population() print(gp.population._random_generations) assert gp.population._random_generations == 1 if pop == P: try: ind = gp.random_offspring() gp.replace(ind) assert False except RuntimeError: pass else: for i in range(3): gp.replace(gp.random_offspring()) assert gp.population.generation == 2
def test_random_generations(): from EvoDAG import EvoDAG from EvoDAG.population import SteadyState class P(SteadyState): def random_selection(self, negative=False): raise RuntimeError('!') y = cl.copy() y[y != 1] = -1 for pop in ['SteadyState', 'Generational', P]: gp = EvoDAG(population_class=pop, all_inputs=True, random_generations=1, early_stopping_rounds=1, popsize=2) gp.X = X gp.y = y gp.create_population() print(gp.population._random_generations) assert gp.population._random_generations == 1 if pop == P: try: ind = gp.random_offspring() gp.replace(ind) assert False except RuntimeError: pass else: for i in range(3): gp.replace(gp.random_offspring()) assert gp.population.generation == 2
def test_multiple_variables(): import numpy as np from EvoDAG.population import Inputs from EvoDAG.cython_utils import SelectNumbers from EvoDAG import EvoDAG from SparseArray import SparseArray y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, popsize=5, share_inputs=True) gp.X = X gp.X[-1]._eval_tr = SparseArray.fromlist( [0 for x in range(gp.X[-1].hy.size())]) gp.nclasses(y) gp.y = y inputs = Inputs(gp, SelectNumbers([x for x in range(len(gp.X))])) inputs._func = [inputs._func[-1]] inputs._nfunc = 1 v = inputs.input() assert v is not None mask = np.array(gp._mask[0].full_array(), dtype=np.bool) D = np.array([x.hy.full_array() for x in gp.X]).T b = np.array(gp._ytr[0].full_array()) coef = np.linalg.lstsq(D[mask], b[mask])[0] for a, b in zip(coef, v.weight[0]): assert_almost_equals(a, b)
def test_inputs_func_argument_regression(): from EvoDAG import EvoDAG class Error: nargs = 2 min_nargs = 2 classification = True regression = True def __init__(self, *args, **kwargs): raise RuntimeError('aqui') y = cl.copy() y[y == 0] = -1 y[y > -1] = 1 gp = EvoDAG(classifier=False, multiple_outputs=False, pr_variable=0, input_functions=[Error], popsize=5, share_inputs=True) gp.X = X gp.nclasses(y) gp.y = y try: gp.create_population() assert False except RuntimeError: pass
def test_multiple_outputs_error_rate_ts(): from EvoDAG import EvoDAG from EvoDAG.node import Add, Min, Max y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, function_set=[Add, Min, Max], early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, fitness_function='ER', seed=0, popsize=100) gp.X = X[:-1] gp.nclasses(y[:-1]) gp.y = y[:-1] gp.create_population() a = gp.random_offspring() hys = SparseArray.argmax(a.hy) hy = np.array(hys.full_array()) # print(((hys - gp._y_klass).sign().fabs() * gp._mask_ts).sum()) mask = np.array(gp._mask_ts.full_array()).astype(np.bool) # print((y[:-1][mask] != hy[mask]).mean()) print(-a.fitness, (y[:-1][mask] != hy[mask]).mean()) assert_almost_equals(-a.fitness, (y[:-1][mask] != hy[mask]).mean())
def test_model_graphviz(): from EvoDAG import RootGP from EvoDAG.node import Function import tempfile y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp = RootGP(generations=3, tournament_size=2, early_stopping_rounds=-1, seed=0, popsize=10).fit(X, y) m = gp.model() print(m._hist) print(m._hist[-1].position, m._hist[-1]._variable) with tempfile.TemporaryFile('w+') as io: m.graphviz(io) io.seek(0) l = io.readlines() cnt = len(m._hist) for k in m._hist: if isinstance(k, Function): v = k._variable if isinstance(v, list): cnt += len(v) else: cnt += 1 print("".join(l)) print(cnt, len(l)) assert 2 + cnt == len(l)
def test_popsize_nvar(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG.init(popsize='nvar', time_limit=5) print(X.shape) gp.fit(X, y) default_nargs() assert gp.population._popsize == (X.shape[1] + len(gp._input_functions))
def test_share_inputs(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, popsize=5, share_inputs=True) gp.fit(X, y) assert gp._share_inputs
def create_problem_node2(nargs=4, seed=0): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=nargs, multiple_outputs=True, seed=seed) gp.X = X gp.Xtest = X y = cl.copy() gp.nclasses(y) gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def create_problem_node(nargs=4): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=4) gp.X = X gp.Xtest = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def test_random_selection(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 RootGP(generations=np.inf, tournament_size=1, early_stopping_rounds=-1, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:])
def create_problem_node(nargs=4, seed=0): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=nargs, classifier=False, seed=seed) gp.X = X gp.Xtest = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def test_random_selection(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 RootGP(generations=np.inf, tournament_size=1, early_stopping_rounds=-1, classifier=False, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:])
def test_macro_F1(): from EvoDAG.cython_utils import Score from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=2, popsize=1000) gp.y = y gp.X = X gp.create_population() off = gp.random_offspring() hy = SparseArray.argmax(off.hy) index = np.array(gp._mask_ts.index) y = np.array(gp._y_klass.full_array())[index] hy = np.array(hy.full_array())[index] nclasses = gp._bagging_fitness.nclasses precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)]) recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)]) print(precision, recall) f1 = Score(nclasses) mf1, mf1_v = f1.a_F1(gp._y_klass, SparseArray.argmax(off.hy), gp._mask_ts.index) for x, y in zip(precision, f1.precision): if not np.isfinite(x): continue assert_almost_equals(x, y) for x, y in zip(recall, f1.recall): if not np.isfinite(x): continue assert_almost_equals(x, y) _ = (2 * precision * recall) / (precision + recall) m = ~np.isfinite(_) _[m] = 0 assert_almost_equals(np.mean(_), mf1) print(f1.precision, f1.recall, mf1, mf1_v) gp._fitness_function = 'macro-F1' gp._bagging_fitness.set_fitness(off) assert_almost_equals(off.fitness, mf1 - 1) assert_almost_equals(off.fitness_vs, mf1_v - 1) index = np.array(gp._mask_ts.full_array()) == 0 y = np.array(gp._y_klass.full_array())[index] hy = SparseArray.argmax(off.hy) hy = np.array(hy.full_array())[index] precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)]) recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)]) _ = (2 * precision * recall) / (precision + recall) m = ~np.isfinite(_) _[m] = 0 assert_almost_equals(np.mean(_) - 1, off.fitness_vs)
def test_input_functions(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG.init( input_functions=["NaiveBayes", "NaiveBayesMN", "Centroid"], Centroid=2, time_limit=5) input_functions = [x for x in gp._input_functions] gp.fit(X, y) default_nargs() for a, b in zip(input_functions, gp.population.hist[:3]): print(b, a) assert isinstance(b, a)
def test_min_class(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=0, popsize=100) gp.y = y[:-1] gp.X = X[:-1] assert gp._bagging_fitness.min_class == 2
def test_SteadyState_generation(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 gp = EvoDAG(population_class='SteadyState', all_inputs=True, early_stopping_rounds=1, popsize=2) gp.X = X gp.y = y gp.create_population() for i in range(3): gp.replace(gp.random_offspring()) assert gp.population.generation == 2
def test_transform_to_mo(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=0, popsize=10000) gp.nclasses(y) k = np.unique(y) y = gp._bagging_fitness.transform_to_mo(y) assert k.shape[0] == y.shape[1]
def create_problem_node2(nargs=4, seed=0): from EvoDAG import RootGP from test_root import X, cl import numpy as np gp = RootGP(generations=1, popsize=nargs, multiple_outputs=True, seed=seed) X1 = np.concatenate((X, np.atleast_2d(np.zeros(X.shape[0])).T), axis=1) for i in range(10, 20): X1[i, -1] = 1 gp.X = X1 gp.Xtest = X1 y = cl.copy() gp.nclasses(y) gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def test_multiple_outputs2(): from EvoDAG import EvoDAG from EvoDAG.model import Model y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=0, popsize=10000).fit(X, y, test_set=X) m = gp.model() assert isinstance(m, Model) assert len(gp.y) == 3
def test_SteadyState_generation(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 gp = EvoDAG(population_class='SteadyState', all_inputs=True, classifier=False, early_stopping_rounds=1, popsize=2) gp.X = X gp.y = y gp.create_population() for i in range(3): gp.replace(gp.random_offspring()) assert gp.population.generation == 2
def test_multiple_outputs_decision_function(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, multiple_outputs=True, early_stopping_rounds=-1, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:]) m = gp.model() assert m.multiple_outputs hy = m.decision_function(X) assert len(hy) == 3 for i in hy: assert i.isfinite()
def test_multiple_outputs(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=0, popsize=10000) gp.X = X gp.nclasses(y) gp.y = y gp.create_population() assert len(gp.y) == 3
def test_HGeneration_pr_variable(): import json from EvoDAG.utils import RandomParameterSearch from EvoDAG import EvoDAG get_remote_data() params = json.loads(open('evodag.params').read()) params['population_class'] = 'HGenerational' params['pr_variable'] = 1.0 kw = RandomParameterSearch.process_params(params) y = cl.copy() try: EvoDAG(**kw).fit(X, y) except AssertionError: return assert False
def test_multiple_outputs_predict(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, multiple_outputs=True, early_stopping_rounds=-1, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:]) m = gp.model() assert m.multiple_outputs hy = m.predict(X) u = np.unique(y) for i in np.unique(hy): assert i in u
def test_mask(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(generations=np.inf, tournament_size=2, early_stopping_rounds=100, time_limit=0.9, multiple_outputs=True, seed=0, popsize=100) gp.y = y gp.X = X gp.create_population() ts = np.array(gp._mask_ts.full_array()) == 0 vs = np.array(gp._mask_vs.full_array()) == 1 assert np.all(ts == vs)
def test_all_inputs2(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 gp = EvoDAG(population_class='Generational', all_inputs=True, popsize=3) gp.X = X gp.y = y gp.create_population() print(len(gp.population.population), len(gp.X)) assert len(gp.population.population) == len(gp.X) for i in range(gp.popsize): a = gp.random_offspring() gp.replace(a) assert len(gp.population.population) == gp.popsize
def test_all_inputs(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 for pc in ['Generational', 'SteadyState']: gp = EvoDAG(population_class=pc, all_inputs=True, popsize=10) gp.X = X gp.y = y gp.create_population() assert len(gp.population.population) < 10 for i in range(gp.population.popsize, gp.population._popsize): a = gp.random_offspring() gp.replace(a) assert len(gp.population.population) == 10
def test_pickle_model(): from EvoDAG import RootGP import pickle y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:]) m = gp.model() hy = gp.decision_function(X=X[-10:]) m1 = pickle.loads(pickle.dumps(m)) hy1 = m1.decision_function(X=X[-10:]) assert hy.SSE(hy1) == 0
def test_all_inputs(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 for pc in ['Generational', 'SteadyState']: gp = EvoDAG(population_class=pc, all_inputs=True, classifier=False, popsize=10) gp.X = X gp.y = y gp.create_population() assert len(gp.population.population) < 10 for i in range(gp.population.popsize, gp.population._popsize): a = gp.random_offspring() gp.replace(a) assert len(gp.population.population) == 10
def test_inputs_func_argument(): from EvoDAG import EvoDAG class Error: nargs = 2 min_nargs = 2 classification = True regression = True def __init__(self, *args, **kwargs): raise RuntimeError('aqui') y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, pr_variable=0, input_functions=[Error], popsize=5, share_inputs=True) gp.X = X gp.nclasses(y) gp.y = y try: gp.create_population() assert False except RuntimeError: pass gp = EvoDAG( classifier=True, multiple_outputs=True, pr_variable=0, input_functions=['NaiveBayes', 'NaiveBayesMN', 'MultipleVariables'], popsize=5, share_inputs=True).fit(X, y) assert gp try: EvoDAG(classifier=True, multiple_outputs=True, pr_variable=0, input_functions=[ 'NaiveBayesXX', 'NaiveBayesMN', 'MultipleVariables' ], popsize=5, share_inputs=True).fit(X, y) except AttributeError: pass
def test_model_nvar(): from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, popsize=5, share_inputs=True) gp.fit(X, y) assert gp._share_inputs m = gp.model() print(X.shape) assert m.nvar == X.shape[1] try: m.predict(X[:, :3]) assert False except RuntimeError: pass
def test_model_hist(): from EvoDAG import RootGP from EvoDAG.base import Model y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, seed=1, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:]) hist = gp.population.hist trace = gp.trace(gp.population.estopping) a = hist[trace[-1]].variable m = Model(trace, hist) print((m._map, a, m._hist[-1].variable)) for v1, v2 in zip(a, m._hist[-1].variable): assert m._map[v1] == v2
def test_all_init_popsize(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 gp = EvoDAG(population_class='Generational', all_inputs=True, early_stopping_rounds=1, popsize=2) gp.X = X gp.y = y gp.create_population() assert gp.init_popsize == len(gp.X) gp = EvoDAG(population_class='Generational', # all_inputs=True, early_stopping_rounds=1, popsize=2) gp.X = X gp.y = y gp.create_population() assert gp.init_popsize == gp.popsize
def test_ensemble(): from EvoDAG import RootGP from EvoDAG.model import Ensemble from EvoDAG.node import Add y = cl.copy() gps = [RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, seed=seed, popsize=10).fit(X[:-10], y[:-10], test_set=X) for seed in range(2, 5)] ens = Ensemble([gp.model() for gp in gps]) res = [gp.decision_function() for gp in gps] res = [Add.cumsum([x[j] for x in res]) for j in range(3)] res = [x / 3. for x in res] r2 = ens.decision_function(None) for a, b in zip(res, r2): assert a.SSE(b) == 0 r2 = ens.predict(None)
def test_clean(): from EvoDAG import EvoDAG y = cl.copy() y[y != 1] = -1 for pc in ['Generational', 'SteadyState']: gp = EvoDAG(population_class=pc, popsize=5) gp.X = X gp.y = y gp.create_population() for i in range(10): v = gp.random_offspring() gp.replace(v) pop = gp.population.population esi = gp.population.estopping for i in gp.population._hist: print(i == esi, i in pop, i, '-'*10, i.fitness) if i == esi: assert i.hy is not None elif i in pop: assert i.hy is not None assert gp.population.estopping.hy is not None
def test_ensemble_model(): from EvoDAG import RootGP from EvoDAG.model import Ensemble from EvoDAG.node import Add y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gps = [RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, seed=seed, popsize=10).fit(X[:-10], y[:-10], test_set=X) for seed in range(3)] ens = Ensemble([gp.model() for gp in gps]) res = [gp.decision_function() for gp in gps] res = Add.cumsum(res) / 3 r2 = ens.decision_function(None) assert res.SSE(r2) == 0 a = SparseArray.fromlist(ens.predict(None)) assert r2.sign().SSE(a) == 0
def test_generational_generation(): from EvoDAG.population import Generational from EvoDAG import EvoDAG gp = EvoDAG(population_class='Generational', popsize=10) gp.X = X y = cl.copy() y[y != 1] = -1 gp.y = y gp.create_population() assert isinstance(gp.population, Generational) p = [] for i in range(gp.popsize-1): a = gp.random_offspring() p.append(a) gp.replace(a) assert len(gp.population._inner) == (gp.popsize - 1) a = gp.random_offspring() p.append(a) gp.replace(a) assert len(gp.population._inner) == 0 for a, b in zip(gp.population.population, p): assert a == b