コード例 #1
0
ファイル: test_gp.py プロジェクト: pombredanne/EvoDAG
def test_gp_population_full():
    Add.nargs = 2
    Mul.nargs = 2
    from EvoDAG.gp import Population
    from EvoDAG import EvoDAG
    fs = EvoDAG()._function_set

    class Population2(Population):
        def __init__(self, *args, **kwargs):
            super(Population2, self).__init__(*args, **kwargs)
            self._funcs = [Add, Sin]
            self._terms = [2, 0]

        def random_function(self):
            func = self._funcs.pop()
            if func.nargs == 1:
                return func(0, weight=1)
            return func(range(func.nargs), weight=np.ones(func.nargs))

        def random_terminal(self):
            return Variable(self._terms.pop(), 1)

    pop = Population2(fs, nterminals=3)
    ind = pop.create_random_ind_full(depth=2)
    assert len(pop._funcs) == 0 and len(pop._terms) == 0
    assert isinstance(ind[0], Sin) and isinstance(ind[1], Add)
    assert ind[2].variable == 0 and ind[3].variable == 2
    ind = Individual(ind)
    print(X.shape, ind.individual)
    hy = ind.decision_function(X)
    assert hy.isfinite()
    default_nargs()
コード例 #2
0
ファイル: test_model.py プロジェクト: yadevi/EvoDAG
def test_init2():
    from EvoDAG.model import Ensemble
    m = Ensemble.init(n_estimators=4, n_jobs=1, seed=10, early_stopping_rounds=100).fit(X, cl)
    hy = m.predict(X)
    print((cl == hy).mean(), cl, hy)
    assert (cl == hy).mean() > 0.9
    default_nargs()
コード例 #3
0
ファイル: test_gp.py プロジェクト: pombredanne/EvoDAG
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_init_evodag():
    from EvoDAG.model import EvoDAG
    m = EvoDAG().fit(X, cl)
    hy = m.predict(X)
    print((cl == hy).mean(), cl, hy)
    assert (cl == hy).mean() > 0.9
    default_nargs()
コード例 #5
0
def test_nargs_function():
    from EvoDAG.node import Mul
    default_nargs()
    Add.nargs = 4
    assert Add.nargs == 4
    assert Mul.nargs == 2
    default_nargs()
コード例 #6
0
def test_init_e():
    from EvoDAG.model import EvoDAGE
    m = EvoDAGE(n_estimators=4, n_jobs=4, seed=10,
                early_stopping_rounds=100).fit(X, cl)
    hy = m.predict(X)
    assert (cl == hy).mean() > 0.9
    default_nargs()
コード例 #7
0
def test_init_regression():
    from EvoDAG.model import Ensemble
    m = Ensemble.init(n_estimators=4, n_jobs=4, seed=10,
                      classifier=False).fit(X, cl)
    hy = m.predict(X)
    assert np.unique(hy).shape[0] > 3
    default_nargs()
コード例 #8
0
def test_predict_proba():
    from EvoDAG.model import EvoDAGE
    m = EvoDAGE(n_estimators=3, n_jobs=2, time_limit=4).fit(X, cl)
    pr = m.predict_proba(X)
    default_nargs()
    print(pr.min(), pr.max())
    assert pr.min() >= 0 and pr.max() <= 1
コード例 #9
0
def test_init_evodag_extras():
    from EvoDAG import EvoDAG
    from test_command_line import default_nargs
    m = EvoDAG.init(seed=10, popsize=10,
                    early_stopping_rounds=10).fit(X, cl)
    assert m.popsize == 10
    default_nargs()
コード例 #10
0
def test_raw_decision_function():
    from EvoDAG.model import EvoDAGE
    m = EvoDAGE(n_estimators=3, n_jobs=2, time_limit=4)
    m.fit(X, cl)
    pr = m.raw_decision_function(X)
    default_nargs()
    print(pr.shape)
    assert pr.shape[1] == np.unique(cl).shape[0] * len(m._m.models)
コード例 #11
0
ファイル: test_population.py プロジェクト: yadevi/EvoDAG
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))
コード例 #12
0
def test_init2():
    from EvoDAG.model import Ensemble
    m = Ensemble.init(n_estimators=4, n_jobs=1, seed=10).fit(X, cl)
    hy = m.predict(X)
    print([x.full_array() for x in m.decision_function(X)])
    print((cl == hy).mean(), cl, hy)
    assert (cl == hy).mean() > 0.9
    default_nargs()
コード例 #13
0
def test_init_time_limit():
    from EvoDAG.model import EvoDAGE
    import time
    local = time.time()
    EvoDAGE(n_estimators=30, n_jobs=2, time_limit=4).fit(X, cl)
    default_nargs()
    t = time.time() - local
    print(t)
    assert t <= 6
コード例 #14
0
def test_X_list():
    from EvoDAG import EvoDAG
    from test_command_line import default_nargs
    m = EvoDAG.init(seed=10, popsize=10,
                    early_stopping_rounds=10).fit(X.tolist(), cl)
    assert m.popsize == 10
    default_nargs()
    print(X.shape, len(m.X))
    assert len(m.X) == 4
コード例 #15
0
def test_test_set_shuffle():
    from EvoDAG import EvoDAG
    from test_command_line import default_nargs
    m = EvoDAG.init(seed=10, popsize=10,
                    early_stopping_rounds=10).fit(X.tolist(), cl, test_set='shuffle')
    assert m.popsize == 10
    default_nargs()
    print(X.shape, len(m.X))
    assert len(m.X) == 4
    v = m.population.hist[-1]
    assert v.hy_test and len(v.hy_test) == np.unique(cl).shape[0]
コード例 #16
0
def test_normalize_naiveMN():
    from EvoDAG import EvoDAG as evodag
    m = evodag.init(time_limit=4)
    m.fit(X, cl)
    hy = [x for x in m.population.hist[1].hy]
    naive = m.model(v=m.population.hist[1])
    df = np.array([x.full_array() for x in naive.decision_function(X)]).T
    hy = np.array([x.full_array() for x in hy]).T
    hy = hy / np.atleast_2d(hy.sum(axis=1)).T * 2 - 1
    for a, b in zip(hy, df):
        [assert_almost_equals(v, w) for v, w in zip(a, b)]
    default_nargs()
コード例 #17
0
ファイル: test_population.py プロジェクト: yadevi/EvoDAG
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)
コード例 #18
0
def test_init_evodag_params_dict():
    from EvoDAG import EvoDAG
    from test_command_line import default_nargs
    import os
    import json
    kw = os.path.join(os.path.dirname(__file__), '..',
                      'conf', 'default_parameters.json')
    with open(kw) as fpt:
        kw = json.loads(fpt.read())
    m = EvoDAG.init(params_fname=kw, seed=10, popsize=10,
                    early_stopping_rounds=10).fit(X, cl)
    assert m.popsize == 10
    default_nargs()
コード例 #19
0
ファイル: test_model.py プロジェクト: yadevi/EvoDAG
def test_max_training_size():
    from EvoDAG.model import EvoDAGE
    import time
    local = time.time()
    try:
        EvoDAGE(n_estimators=30, n_jobs=2, time_limit=4,
                max_training_size="hola").fit(X, cl)
    except TypeError:
        default_nargs()
        t = time.time() - local
        print(t)
        assert t <= 6
        return
    assert False        
コード例 #20
0
def test_bug_naive_bayes():
    from EvoDAG import EvoDAG
    from test_command_line import default_nargs
    Xt = X.copy()
    y = cl.copy()
    mask = np.zeros(y.shape[0], dtype=np.bool)
    mask[y == 0] = True
    mask[y == 1] = True
    mask[np.where(y == 2)[0][:2]] = True
    m = EvoDAG.init(seed=11, popsize=10, fitness_function='macro-F1',
                    early_stopping_rounds=10).fit(Xt[mask], y[mask])
    assert m.popsize == 10
    default_nargs()
    print(X.shape, len(m.X))
    assert len(m.X) == 4
コード例 #21
0
def test_normalize_Centroid():
    from EvoDAG import EvoDAG as evodag
    m = evodag.init(time_limit=4)
    m.fit(X, cl)
    if not isinstance(m.population.hist[2].hy, list):
        return
    hy = [x for x in m.population.hist[2].hy]
    naive = m.model(v=m.population.hist[2])
    df = np.array([x.full_array() for x in naive.decision_function(X)]).T
    hy = np.array([x.full_array() for x in hy]).T
    hy = np.exp(hy) * 2 - 1
    print(df, hy)
    for a, b in zip(hy, df):
        [assert_almost_equals(v, w) for v, w in zip(a, b)]
    default_nargs()
コード例 #22
0
ファイル: test_gp.py プロジェクト: pombredanne/EvoDAG
def test_gp_create_population():
    Add.nargs = 2
    Mul.nargs = 2
    from EvoDAG.node import NaiveBayes, NaiveBayesMN
    from EvoDAG.gp import Population
    from EvoDAG import EvoDAG
    fs = [
        x for x in EvoDAG()._function_set
        if not (x is NaiveBayes or x is NaiveBayesMN)
    ]
    print([(x, x.nargs) for x in fs])
    pop = Population(fs, nterminals=X.shape[1])
    inds = pop.create_population(X=X)
    for i in inds:
        assert i[0].hy.isfinite()
    default_nargs()
コード例 #23
0
def test_probability_calibration():
    class C(object):
        def __init__(self):
            pass

        def fit(self, X, y):
            self._X = X
            return self

        def predict_proba(self, X):
            return np.array([[0, 1], [0.5, 0.5], [1, 0]])

    from EvoDAG import EvoDAG as evodag
    m = evodag.init(time_limit=4,
                    early_stopping_rounds=10,
                    probability_calibration=C).fit(X, cl)
    model = m.model()
    hy = model.predict_proba(X)
    pr = np.array([[0, 1], [0.5, 0.5], [1, 0]])
    assert np.fabs(hy - pr).sum() == 0
    default_nargs()
コード例 #24
0
def test_probability_calibration_ensemble():
    class C(object):
        def __init__(self):
            pass

        def fit(self, X, y):
            self._X = X
            return self

        def predict_proba(self, X):
            return np.array([[0, 1], [0.5, 0.5], [1, 0]])

    from EvoDAG.model import EvoDAGE as evodag
    model = evodag(time_limit=4,
                   early_stopping_rounds=10,
                   probability_calibration=C).fit(X, cl)
    for m in model.models:
        assert isinstance(m._probability_calibration, C)
    assert model.probability_calibration
    hy = model.predict_proba(X)
    pr = np.array([[0, 1], [0.5, 0.5], [1, 0]])
    assert np.fabs(hy - pr).sum() == 0
    default_nargs()
コード例 #25
0
def test_height():
    from EvoDAG.node import Mul, NaiveBayesMN, NaiveBayes
    from test_command_line import default_nargs
    from EvoDAG import EvoDAG
    # NaiveBayesMN.nargs = 0
    # NaiveBayes.nargs = 0
    gp = EvoDAG(generations=1,
                seed=1,
                multiple_outputs=True,
                tournament_size=2,
                use_all_vars_input_functions=True,
                input_functions=[NaiveBayes],
                popsize=5)
    gp.X = X
    gp.nclasses(cl)
    gp.y = cl.copy()
    gp.create_population()
    print(NaiveBayes.nargs, NaiveBayesMN.nargs)
    print([(x, x.height) for x in gp.population.population])
    assert np.all([x.height == 0 for x in gp.population.population])
    args = [3, 4]
    f = gp._random_offspring(Mul, args)
    assert f.height == 1
    default_nargs()
コード例 #26
0
def test_init():
    from EvoDAG.model import Ensemble
    m = Ensemble.init(n_estimators=4, n_jobs=4, seed=10).fit(X, cl)
    hy = m.predict(X)
    assert (cl == hy).mean() > 0.9
    default_nargs()