Ejemplo n.º 1
0
def test_g_recall():
    from EvoDAG import EvoDAG
    y = cl.copy()
    gp = EvoDAG(generations=np.inf,
                tournament_size=2,
                early_stopping_rounds=100,
                time_limit=0.9,
                multiple_outputs=True,
                seed=0,
                popsize=500)
    gp.y = y
    gp.X = X
    gp.create_population()
    off = gp.random_offspring()
    hy = SparseArray.argmax(off.hy)
    index = np.array(gp._mask_ts.index)
    y = np.array(gp._y_klass.full_array())[index]
    hy = np.array(hy.full_array())[index]
    nclasses = gp._bagging_fitness.nclasses
    recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)])
    score = np.prod(recall) - 1
    gp._fitness_function = 'g_recall'
    gp._bagging_fitness.set_fitness(off)
    assert_almost_equals(score, off.fitness)
    index = np.array(gp._mask_ts.full_array()) == 0
    y = np.array(gp._y_klass.full_array())[index]
    hy = SparseArray.argmax(off.hy)
    hy = np.array(hy.full_array())[index]
    recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)])
    score = np.prod(recall) - 1
    assert_almost_equals(score, off.fitness_vs)
Ejemplo n.º 2
0
def test_a_precision():
    from EvoDAG.cython_utils import Score
    from EvoDAG import EvoDAG
    y = cl.copy()
    gp = EvoDAG(generations=np.inf,
                tournament_size=2,
                early_stopping_rounds=100,
                time_limit=0.9,
                multiple_outputs=True,
                seed=0,
                popsize=500)
    gp.y = y
    gp.X = X
    gp.create_population()
    # off = gp.random_offspring()
    off = gp.population.bsf
    hy = SparseArray.argmax(off.hy)
    index = np.array(gp._mask_ts.index)
    y = np.array(gp._y_klass.full_array())[index]
    hy = np.array(hy.full_array())[index]
    nclasses = gp._bagging_fitness.nclasses
    precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)])
    f1 = Score(nclasses)
    mf1, mf1_v = f1.a_precision(gp._y_klass, SparseArray.argmax(off.hy),
                                gp._mask_ts.index)
    assert_almost_equals(np.mean(precision), mf1)
    gp._fitness_function = 'a_precision'
    gp._bagging_fitness.set_fitness(off)
    assert_almost_equals(mf1 - 1, off.fitness)
    index = np.array(gp._mask_ts.full_array()) == 0
    y = np.array(gp._y_klass.full_array())[index]
    hy = SparseArray.argmax(off.hy)
    hy = np.array(hy.full_array())[index]
    precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)])
    assert_almost_equals(np.mean(precision) - 1, off.fitness_vs)
Ejemplo n.º 3
0
def test_macro_F1():
    from EvoDAG.cython_utils import Score
    from EvoDAG import EvoDAG
    y = cl.copy()
    gp = EvoDAG(generations=np.inf,
                tournament_size=2,
                early_stopping_rounds=100,
                time_limit=0.9,
                multiple_outputs=True,
                seed=2,
                popsize=1000)
    gp.y = y
    gp.X = X
    gp.create_population()
    off = gp.random_offspring()
    hy = SparseArray.argmax(off.hy)
    index = np.array(gp._mask_ts.index)
    y = np.array(gp._y_klass.full_array())[index]
    hy = np.array(hy.full_array())[index]
    nclasses = gp._bagging_fitness.nclasses
    precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)])
    recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)])
    print(precision, recall)
    f1 = Score(nclasses)
    mf1, mf1_v = f1.a_F1(gp._y_klass, SparseArray.argmax(off.hy),
                         gp._mask_ts.index)
    for x, y in zip(precision, f1.precision):
        if not np.isfinite(x):
            continue
        assert_almost_equals(x, y)
    for x, y in zip(recall, f1.recall):
        if not np.isfinite(x):
            continue
        assert_almost_equals(x, y)
    _ = (2 * precision * recall) / (precision + recall)
    m = ~np.isfinite(_)
    _[m] = 0
    assert_almost_equals(np.mean(_), mf1)
    print(f1.precision, f1.recall, mf1, mf1_v)
    gp._fitness_function = 'macro-F1'
    gp._bagging_fitness.set_fitness(off)
    assert_almost_equals(off.fitness, mf1 - 1)
    assert_almost_equals(off.fitness_vs, mf1_v - 1)
    index = np.array(gp._mask_ts.full_array()) == 0
    y = np.array(gp._y_klass.full_array())[index]
    hy = SparseArray.argmax(off.hy)
    hy = np.array(hy.full_array())[index]
    precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)])
    recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)])
    _ = (2 * precision * recall) / (precision + recall)
    m = ~np.isfinite(_)
    _[m] = 0
    assert_almost_equals(np.mean(_) - 1, off.fitness_vs)
Ejemplo n.º 4
0
def test_F1():
    from EvoDAG.cython_utils import Score
    from EvoDAG import EvoDAG
    y = cl.copy()
    gp = EvoDAG(generations=np.inf,
                tournament_size=2,
                early_stopping_rounds=100,
                time_limit=0.9,
                multiple_outputs=True,
                seed=0,
                popsize=500)
    gp.y = y
    gp.X = X
    gp.create_population()
    off = gp.random_offspring()
    hy = SparseArray.argmax(off.hy)
    index = np.array(gp._mask_ts.index)
    y = np.array(gp._y_klass.full_array())[index]
    hy = np.array(hy.full_array())[index]
    nclasses = gp._bagging_fitness.nclasses
    precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)])
    recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)])
    f1 = Score(nclasses)
    assert gp._bagging_fitness.min_class >= 0 and gp._bagging_fitness.min_class < gp._bagging_fitness.nclasses
    mf1, mf1_v = f1.F1(gp._bagging_fitness.min_class, gp._y_klass,
                       SparseArray.argmax(off.hy), gp._mask_ts.index)

    _ = (2 * precision * recall) / (precision + recall)
    m = ~np.isfinite(_)
    _[m] = 0
    assert_almost_equals(_[gp._bagging_fitness.min_class], mf1)
    gp._fitness_function = 'F1'
    gp._bagging_fitness.set_fitness(off)
    assert_almost_equals(mf1 - 1, off.fitness)
    index = np.array(gp._mask_ts.full_array()) == 0
    y = np.array(gp._y_klass.full_array())[index]
    hy = SparseArray.argmax(off.hy)
    hy = np.array(hy.full_array())[index]
    precision = np.array([(y[hy == k] == k).mean() for k in range(nclasses)])
    recall = np.array([(hy[y == k] == k).mean() for k in range(nclasses)])
    _ = (2 * precision * recall) / (precision + recall)
    m = ~np.isfinite(_)
    _[m] = 0
    assert_almost_equals(_[gp._bagging_fitness.min_class] - 1, off.fitness_vs)