def test_indindividual_decision_function(): Add.nargs = 2 Mul.nargs = 2 vars = Model.convert_features(X) for x in vars: x._eval_ts = x._eval_tr.copy() vars = [Variable(k, weight=np.ones(1)) for k in range(len(vars))] for i in range(len(vars)): ind = Individual([vars[i]]) ind.decision_function(X) hy = tonparray(ind._ind[0].hy) [assert_almost_equals(a, b) for a, b in zip(X[:, i], hy)] ind = Individual([ Sin(0, weight=np.ones(1)), Add(range(2), np.ones(2)), vars[0], vars[-1] ]) ind.decision_function(X) print(ind._ind[0].hy, ind._ind[1].hy) hy = tonparray(ind._ind[0].hy) y = np.sin(X[:, 0] + X[:, -1]) [assert_almost_equals(a, b) for a, b in zip(y, hy)] y = np.sin((X[:, 0] + X[:, 1]) * X[:, 0] + X[:, 2]) ind = Individual([ Sin(0, weight=1), Add(range(2), weight=np.ones(2)), Mul(range(2), weight=1), Add(range(2), weight=np.ones(2)), vars[0], vars[1], vars[0], vars[2] ]) ind.decision_function(X) # assert v.hy.SSE(v.hy_test) == 0 hy = tonparray(ind._ind[0].hy) [assert_almost_equals(a, b) for a, b in zip(hy, y)] default_nargs()
def test_compute_weight(): gp, args = create_problem_node() mask = SparseArray.fromlist(np.ones(len(gp._mask))) n = Add(list(range(len(args))), ytr=gp._ytr, mask=mask) D = np.array([tonparray(i.hy) for i in args]).T coef = n.compute_weight([x.hy for x in args]) r = np.linalg.lstsq(D, tonparray(gp._ytr))[0] [assert_almost_equals(a, b) for a, b in zip(coef, r)]
def test_node_hash(): from EvoDAG.node import Add, Mul add = Add([21, 3]) sets = set() sets.add(add.signature()) add2 = Add([21, 3]) assert add2.signature() in sets assert Add([3, 21]).signature() in sets assert Mul([2, 3]).signature() == Mul([3, 2]).signature()
def test_node_add(): gp, args = create_problem_node() n = Add(list(range(len(args))), ytr=gp._ytr, mask=gp._mask) coef = n.compute_weight([x.hy for x in args]) assert n.eval(args) # a = map(lambda (a, b): a.hy * b, zip(args, coef)) a = [a.hy * b for a, b in zip(args, coef)] r = n.cumsum(a) assert n.hy.SSE(r) == 0 assert n.hy_test.SSE(r) == 0
def test_node_tostore(): gp, args = create_problem_node(nargs=4) Add.nargs = 4 n = Add(list(range(len(args))), ytr=gp._ytr, mask=gp._mask) n.position = 10 assert n.eval(args) n1 = n.tostore() assert n1.nargs == n.nargs assert n1.position == n.position assert np.all(n1.weight == n.weight) assert n1.hy is None
def test_node_pickle(): import pickle import tempfile gp, args = create_problem_node() n = Add(list(range(len(args))), ytr=gp._ytr, mask=gp._mask) n.position = 10 assert n.eval(args) with tempfile.TemporaryFile('w+b') as io: pickle.dump(n, io) io.seek(0) n1 = pickle.load(io) assert n1._mask.SSE(n._mask) == 0
def test_node_add(): gp, args = create_problem_node() n = Add(list(range(len(args))), ytr=gp._ytr, mask=gp._mask) D = np.array([tonparray(i.hy) for i in args]).T coef = n.compute_weight([x.hy for x in args]) assert n.eval(args) # a = map(lambda (a, b): a.hy * b, zip(args, coef)) a = [_a.hy * b for _a, b in zip(args, coef)] r = n.cumsum(a) print((D * coef).sum(axis=1)[:4], "*") print(tonparray(n.hy)[:4]) print(tonparray(r)[:4]) print(tonparray(gp._mask)[:4]) assert n.hy.SSE(r) == 0 assert n.hy_test.SSE(r) == 0
def test_BER(): from EvoDAG.node import Add from EvoDAG import RootGP from EvoDAG.utils import BER gp = RootGP(generations=1, popsize=4, multiple_outputs=True) assert gp._classifier gp.X = X y = cl.copy() gp.nclasses(y) gp.y = y m = np.sign(tonparray(gp._mask_ts)).astype(np.bool) v = gp.random_leaf() v1 = gp.random_leaf() v1 = gp.random_leaf() a = Add([0, 1], ytr=gp._ytr, mask=gp._mask) a.eval([v, v1]) hy = SparseArray.argmax(a.hy) b = BER(y[m], tonparray(hy)[m]) gp._bagging_fitness.fitness(a) print(b, a.fitness * 100) assert_almost_equals(b, -a.fitness * 100)
def test_BER(): from EvoDAG.node import Add from EvoDAG import RootGP from EvoDAG.utils import BER gp = RootGP(generations=1, popsize=4) assert gp._classifier gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y m = ~ gp._mask.tonparray().astype(np.bool) v = gp.random_leaf() v1 = gp.random_leaf() v1 = gp.random_leaf() a = Add([0, 1], ytr=gp._ytr, mask=gp._mask) a.eval([v, v1]) hy = a.hy.sign() b = BER(y[m], hy.tonparray()[m]) gp.fitness_vs(a) print(b, a.fitness_vs * 100) assert_almost_equals(b, -a.fitness_vs * 100)
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_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, multiple_outputs=True, 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 * (1 / 3.) for x in res] r2 = ens.decision_function(None) for a, b in zip(res, r2): print(a.SSE(b), a.data, b.data, b.full_array()) assert a.SSE(b) == 0
def test_Add_multiple_output(): from EvoDAG.node import Variable gp, args = create_problem_node(nargs=4, seed=0) gp2, _ = create_problem_node(nargs=4, seed=1) ytr = [gp._ytr, gp._ytr] mask = [gp._mask, gp2._mask] vars = [Variable(k, ytr=ytr, mask=mask) for k in range(len(args))] [x.eval(args) for x in vars] add = Add(range(len(vars)), ytr=ytr, mask=mask) assert add.eval(vars) gp, args = create_problem_node(nargs=4, seed=0) vars = [Variable(k, ytr=gp._ytr, mask=gp._mask) for k in range(len(args))] [x.eval(args) for x in vars] add2 = Add(range(len(vars)), ytr=gp._ytr, mask=gp._mask) assert add2.eval(vars) assert add2.hy.SSE(add.hy[0]) == 0 assert add2.hy_test.SSE(add.hy_test[0]) == 0
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_Add_multiple_output2(): from EvoDAG.node import Variable gp, args = create_problem_node(nargs=4, seed=0) for i in args: i.hy_test = None gp2, _ = create_problem_node(nargs=4, seed=1) ytr = [gp._ytr, gp._ytr] mask = [gp._mask, gp2._mask] vars = [Variable(k, ytr=ytr, mask=mask) for k in range(len(args))] [x.eval(args) for x in vars] add = Add(range(len(vars)), ytr=ytr, mask=mask) assert add.eval(vars) vars = [Variable(k, ytr=gp._ytr, mask=gp._mask) for k in range(len(args))] [x.eval(args) for x in vars] add2 = Add(range(len(vars)), ytr=gp._ytr, mask=gp._mask) assert add2.eval(vars) assert add2.hy.SSE(add.hy[0]) == 0 assert isinstance(add.weight, list) and len(add.weight) == 2 assert isinstance(add2.weight, np.ndarray)