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)
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)