Пример #1
0
def test_calc_fitness_shape():
    """test_evaluation.py: calc_fitness correct shapes """
    # load test data set
    boston = load_boston()
    # boston.data = boston.data[::10]
    # boston.target = boston.target[::10]
    n_features = boston.data.shape[1]
    # terminal set
    term_set = []
    # numbers represent column indices of features
    for i in np.arange(n_features):
        term_set.append(node('x', loc=i))  # features
        # term_set.append(('k',0,np.random.rand())) # ephemeral random constants

    # initialize population
    pop_size = 5
    few = FEW(population_size=pop_size, seed_with_ml=False)
    few.term_set = term_set
    few.n_features = n_features
    pop = few.init_pop()

    pop.X = np.asarray(
        list(map(lambda I: few.out(I, boston.data), pop.individuals)))

    fitnesses = few.calc_fitness(pop.X, boston.target, 'mse', 'tournament')
    assert len(fitnesses) == len(pop.individuals)

    # test vectorized fitnesses
    vec_fitnesses = few.calc_fitness(pop.X, boston.target, 'mse', 'lexicase')
    fitmat = np.asarray(vec_fitnesses)
    print("fitmat.shape:", fitmat.shape)
    assert fitmat.shape == (len(pop.individuals), boston.target.shape[0])
Пример #2
0
def test_out_shapes():
    """test_evaluation.py: program output is correct size """
    # load test data set
    boston = load_boston()
    # boston.data = boston.data[::10]
    # boston.target = boston.target[::10]
    n_features = boston.data.shape[1]
    # function set

    # terminal set
    term_set = []
    # numbers represent column indices of features
    for i in np.arange(n_features):
        term_set.append(node('x', loc=i))  # features
        # term_set.append(('k',0,np.random.rand())) # ephemeral random constants

    # initialize population
    pop_size = 5
    few = FEW(population_size=pop_size, seed_with_ml=False)
    few.term_set = term_set
    few.n_features = n_features
    pop = few.init_pop()

    pop.X = np.asarray(
        list(map(lambda I: few.out(I, boston.data), pop.individuals)))

    #pop.X = out(pop.individuals[0],boston.data,boston.target)
    print("pop.X.shape:", pop.X.shape)
    print("boston.target.shape", boston.target.shape)
    assert pop.X.shape == (pop_size, boston.target.shape[0])
Пример #3
0
def test_pop_init():
    """test_population.py: population initialization makes valid trees """
    # define function set
    # function set
    # func_set = [('+',2),('-',2),('*',2),('/',2),('sin',1),('cos',1),('exp',1),('log',1)]
    # terminal set
    term_set = []
    n_features = 3
    # numbers represent column indices of features
    term_set = [node('x',loc=i) for i in np.arange(n_features)]
        # term_set.append(('erc',0,np.random.rand())) # ephemeral random constants
    few = FEW(seed_with_ml=False)
    few.term_set = term_set
    few.n_features=n_features
    pop = few.init_pop()

    for I in pop.individuals:
        assert is_valid_program(I.stack)