Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
def test_regression():
    from EvoDAG import RootGP
    x = np.linspace(-1, 1, 100)
    y = 4.3 * x**2 + 3.2 * x - 3.2
    gp = RootGP(classifier=False, popsize=10,
                generations=2).fit([SparseArray.fromlist(x)],
                                   y,
                                   test_set=[SparseArray.fromlist(x)])
    model = gp.model()
    yh = gp.predict()
    assert not model._classifier
    yh1 = model.predict(X=[SparseArray.fromlist(x)])
    spf = SparseArray.fromlist
    assert spf(yh).SSE(spf(yh1)) == 0
Пример #4
0
def test_regression():
    from EvoDAG import RootGP
    from EvoDAG.sparse_array import SparseArray
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    gp = RootGP(classifier=False,
                popsize=10,
                generations=2).fit([SparseArray.fromlist(x)], y,
                                   test_set=[SparseArray.fromlist(x)])
    model = gp.model()
    yh = gp.predict()
    assert not model._classifier
    yh1 = model.predict(X=[SparseArray.fromlist(x)])
    spf = SparseArray.fromlist
    assert spf(yh).SSE(spf(yh1)) == 0
Пример #5
0
def test_labels():
    from EvoDAG import RootGP
    y = cl.copy()
    mask = y == 0
    y[mask] = 1
    y[~mask] = 2
    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 = m.predict(X=X[:-10])
    print(np.unique(hy))
    print(np.array([1, 2]))
    assert np.all(np.unique(hy) == np.array([1, 2]))
Пример #6
0
def test_labels():
    from EvoDAG import RootGP
    y = cl.copy()
    mask = y == 0
    y[mask] = 1
    gp = RootGP(generations=np.inf,
                tournament_size=2,
                early_stopping_rounds=-1,
                multiple_outputs=True,
                seed=0,
                popsize=100).fit(X, y, test_set=X)
    m = gp.model()
    hy = m.predict(X=X)
    print(np.unique(hy), np.unique(y))
    print(np.array([1, 2]))
    for k in np.unique(hy):
        assert k in [1, 2]
Пример #7
0
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
Пример #8
0
def test_RSE():
    from EvoDAG import RootGP
    from EvoDAG.utils import RSE as rse
    x = np.linspace(-1, 1, 100)
    y = 4.3 * x**2 + 3.2 * x - 3.2
    y[10:12] = 0
    gp = RootGP(classifier=False, popsize=10,
                generations=2).fit([SparseArray.fromlist(x)],
                                   y,
                                   test_set=[SparseArray.fromlist(x)])
    model = gp.model()
    yh = gp.predict()
    assert not model._classifier
    model.predict(X=[SparseArray.fromlist(x)])
    gp._mask = SparseArray.fromlist([2] * yh.shape[0])
    gp._bagging_fitness.fitness_vs(model._hist[-1])
    print(rse(y, yh), model._hist[-1].fitness_vs)
    assert_almost_equals(rse(y, yh), -model._hist[-1].fitness_vs)
Пример #9
0
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,
                multiple_outputs=True,
                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:])
    for a, b in zip(hy, hy1):
        assert a.SSE(b) == 0
Пример #10
0
def test_RSE():
    from EvoDAG import RootGP
    from EvoDAG.sparse_array import SparseArray
    from EvoDAG.utils import RSE as rse
    x = np.linspace(-1, 1, 100)
    y = 4.3*x**2 + 3.2 * x - 3.2
    y[10:12] = 0
    gp = RootGP(classifier=False,
                popsize=10,
                generations=2).fit([SparseArray.fromlist(x)], y,
                                   test_set=[SparseArray.fromlist(x)])
    model = gp.model()
    yh = gp.predict()
    assert not model._classifier
    model.predict(X=[SparseArray.fromlist(x)])
    gp._mask = SparseArray.fromlist([2] * yh.shape[0])
    gp.fitness_vs(model._hist[-1])
    print(rse(y, yh), model._hist[-1].fitness_vs)
    assert_almost_equals(rse(y, yh),
                         -model._hist[-1].fitness_vs)