Пример #1
0
def test_functions_w_multiple_output():
    from EvoDAG.node import Variable, Mul, Div, Min, Max, Atan2, Hypot
    from EvoDAG.node import Argmax, Argmin
    for ff in [Mul, Div, Min, Max, Atan2, Hypot, Argmax, Argmin]:
        for flag in [False, True]:
            gp, args = create_problem_node(nargs=4, seed=0)
            if flag:
                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]
            mul = ff(range(len(vars)), ytr=ytr, mask=mask)
            assert mul.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]
            mul2 = ff(range(len(vars)), ytr=gp._ytr, mask=gp._mask)
            assert mul2.eval(vars)
            assert mul2.hy.SSE(mul.hy[0]) == 0
            if mul.hy_test is not None:
                assert mul2.hy_test.SSE(mul.hy_test[0]) == 0
Пример #2
0
def test_variable_multiple_output_isfinite():
    from EvoDAG.node import Variable
    gp, args = create_problem_node(nargs=4, seed=0)
    gp2, _ = create_problem_node(nargs=4, seed=1)
    n1 = Variable(0, ytr=gp._ytr, mask=gp._mask)
    n1.eval(args)
    print(n1.weight)
    n = Variable(0, ytr=[gp._ytr, gp._ytr], mask=[gp._mask, gp2._mask])
    n.eval(args)
    n.isfinite()
Пример #3
0
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()
Пример #4
0
def test_naive_bayes_MN():
    import numpy as np
    from EvoDAG.node import Variable, NaiveBayesMN
    from EvoDAG.naive_bayes import NaiveBayes as MN
    gp, args = create_problem_node2(nargs=3, seed=0)
    gp.random_leaf()
    vars = [
        Variable(k,
                 ytr=gp._ytr,
                 y_klass=gp._y_klass,
                 mask=gp._mask,
                 finite=True) for k in range(len(args))
    ]
    [x.eval(args) for x in vars]
    nb = MN(mask=gp._mask_ts, klass=gp._y_klass, nclass=gp._labels.shape[0])
    l = []
    [[l.append(y) for y in x.hy] for x in vars]
    coef = [nb.coef_MN(x) for x in l]
    p_klass = coef[0][1]
    coef = [(k, x[0]) for k, x in enumerate(coef)
            if np.all(np.isfinite(np.array(x[0])))]
    R = []
    for k, p in enumerate(p_klass):
        r = p
        for v, w in coef:
            r += (l[v] * w[k])
        R.append(r)
    naive_bayes = NaiveBayesMN(range(len(vars)),
                               ytr=gp._ytr,
                               naive_bayes=gp._naive_bayes,
                               mask=gp._mask,
                               finite=True)
    naive_bayes.eval(vars)
    for a, b in zip(R, naive_bayes.hy):
        [assert_almost_equals(np.exp(v), w) for v, w in zip(a.data, b.data)]
Пример #5
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
Пример #6
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)
Пример #7
0
def test_functions_extra_args():
    from EvoDAG.node import Variable, Mul, Div, Min, Max, Atan2, Hypot
    from EvoDAG.node import Argmax, Argmin
    for ff in [Mul, Div, Min, Max, Atan2, Hypot, Argmax, Argmin]:
        gp, args = create_problem_node2(nargs=4, seed=0)
        vars = [Variable(k, ytr=gp._ytr, y_klass=gp._y_klass,
                         mask=gp._mask, finite=True)
                for k in range(len(args))]
        [x.eval(args) for x in vars]
        mul = ff(range(len(vars)), ytr=gp._ytr, klass=gp._y_klass,
                 mask=gp._mask, finite=True)
        mul.eval(vars)
Пример #8
0
def test_one_multiple_output():
    from EvoDAG.node import Variable
    from EvoDAG.node import Fabs, Exp, Sqrt, Sin, Cos, Log1p, Sq
    from EvoDAG.node import Acos, Asin, Atan, Tan, Cosh, Sinh, Tanh
    from EvoDAG.node import Acosh, Asinh, Atanh, Expm1, Log, Log2, Log10, Lgamma
    from EvoDAG.node import Sign, Ceil, Floor

    for flag in [False, True]:
        gp, args = create_problem_node(nargs=4, seed=0)
        if flag:
            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]
        vars2 = [
            Variable(k, ytr=gp._ytr, mask=gp._mask) for k in range(len(args))
        ]
        [x.eval(args) for x in vars2]
        for FF in [
                Fabs, Exp, Sqrt, Sin, Cos, Log1p, Sq, Acos, Asin, Atan, Tan,
                Cosh, Sinh, Tanh, Acosh, Asinh, Atanh, Expm1, Log, Log2, Log10,
                Lgamma, Sign, Ceil, Floor
        ]:
            ff = FF(0, ytr=ytr, mask=mask)
            ff.eval(vars)
            ff2 = FF(0, ytr=gp._ytr, mask=gp._mask)
            ff2.eval(vars2)
            print(ff.hy)
            if ff.hy is None:
                continue
            assert isinstance(ff.weight, list) and len(ff.weight) == 2
            assert isinstance(ff2.weight, float)
            hy = ff.hy[0]
            if hy.isfinite():
                print('*', FF)
                assert hy.SSE(ff2.hy) == 0
Пример #9
0
def test_naive_bayes():
    import numpy as np
    from EvoDAG.node import Variable, NaiveBayes
    gp, args = create_problem_node2(nargs=4, seed=0)
    gp.random_leaf()
    vars = [
        Variable(k,
                 ytr=gp._ytr,
                 y_klass=gp._y_klass,
                 mask=gp._mask,
                 finite=True) for k in range(len(args))
    ]
    [x.eval(args) for x in vars]
    naive_bayes = NaiveBayes(range(len(vars)),
                             ytr=gp._ytr,
                             naive_bayes=gp._naive_bayes,
                             mask=gp._mask,
                             finite=True)
    naive_bayes.eval(vars)
    mask = np.array(gp._mask_ts.sign().full_array(), dtype=np.bool)
    klass = np.array(gp._y_klass.full_array())[mask]
    unique_klass = np.unique(klass)
    mean = []
    std2 = []
    p_klass = []
    l = []
    for v in vars:
        l += v.hy
    for v in l:
        var = np.array(v.full_array())[mask]
        mean.append([np.mean(var[k == klass]) for k in unique_klass])
        std2.append([np.var(var[k == klass]) for k in unique_klass])
        p_klass = [(k == klass).mean() for k in unique_klass]
    mean = np.array(mean)
    std2 = np.array(std2)
    p_klass = np.array(p_klass)
    likelihood = []
    for i in range(unique_klass.shape[0]):
        a = np.log(p_klass[i])
        b = -0.5 * np.sum([np.log(2. * np.pi * s[i]) for s in std2])
        _ = [(x + -m[i]).sq() * (1 / s[i]) for x, m, s in zip(l, mean, std2)]
        _ = SparseArray.cumsum(_) * -0.5
        likelihood.append(_ + b + a)
    for a, b in zip(likelihood, naive_bayes.hy):
        [assert_almost_equals(v, w) for v, w in zip(a, b.data)]
Пример #10
0
def test_functions_finite():
    from EvoDAG.node import Variable, Mul, Div, Min, Max, Atan2, Hypot
    from EvoDAG.node import Argmax, Argmin
    for ff in [Mul, Div, Min, Max, Atan2, Hypot, Argmax, Argmin]:
        for flag in [False, True]:
            gp, args = create_problem_node(nargs=4, seed=0)
            for v in args:
                _ = [x for x in v._eval_tr.full_array()]
                _[0] = float('inf')
                _[1] = float('inf')
                _[3] = 0
                _[4] = 0
                v._eval_tr = SparseArray.fromlist(_)
            if flag:
                for i in args:
                    i.hy_test = None
            gp2, _ = create_problem_node(nargs=4, seed=1)
            # ytr = [gp._ytr, gp._ytr]
            ytr = gp._ytr
            # mask = [gp._mask, gp2._mask]
            mask = gp._mask.full_array()
            mask[0] = 1
            mask = SparseArray.fromlist(mask)
            vars = [
                Variable(k, ytr=ytr, mask=mask, finite=flag)
                for k in range(len(args))
            ]
            for x in vars:
                _ = x.eval(args)
                print(mask.full_array())
                assert _ == flag
            if not flag:
                continue
            mul = ff(range(len(vars)), ytr=ytr, mask=mask, finite=False)
            _ = mul.eval(vars)
            if isinstance(mul, Div):
                assert not _
                mul = ff(range(len(vars)), ytr=ytr, mask=mask, finite=True)
                _ = mul.eval(vars)
                assert _
Пример #11
0
 def random_terminal(self):
     return Variable(self._terms.pop(), 1)