Skip to content

Commit

Permalink
fix second issue in #42
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorstephens committed Nov 10, 2017
1 parent 68c50b4 commit 60d0424
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 5 additions & 1 deletion gplearn/genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ def fit(self, X, y, sample_weight=None):
if isinstance(self, TransformerMixin):
# Find the best individuals in the final generation
fitness = np.array(fitness)
hall_of_fame = fitness.argsort()[:self.hall_of_fame]
if self._metric.greater_is_better:
hall_of_fame = fitness.argsort()[:self.hall_of_fame]
else:
hall_of_fame = fitness.argsort()[(self.population_size -
self.hall_of_fame):]
evaluation = np.array([gp.execute(X) for gp in
[self._programs[-1][i] for
i in hall_of_fame]])
Expand Down
18 changes: 11 additions & 7 deletions gplearn/tests/test_genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,28 +1022,32 @@ def test_warm_start():
assert_equal(cold_program, warm_program)


def test_customizied_regressor_metrics():
def test_customized_regressor_metrics():
"""Check whether parameter greater_is_better works fine"""

x_data = rng.uniform(-1, 1, 100).reshape(50, 2)
y_true = x_data[:, 0] ** 2 + x_data[:, 1] ** 2

est_gp = SymbolicRegressor(metric='mean absolute error', stopping_criteria=0.000001, random_state=415,
parsimony_coefficient=0.001, verbose=0, init_method='full', init_depth=(2, 4))
est_gp = SymbolicRegressor(metric='mean absolute error',
stopping_criteria=0.000001, random_state=415,
parsimony_coefficient=0.001, init_method='full',
init_depth=(2, 4))
est_gp.fit(x_data, y_true)
formula = est_gp.__str__()
assert_equal("add(mul(X1, X1), mul(X0, X0))", formula, True)

def neg_mean_absolute_error(y, y_pred, sample_weight):
return -1 * mean_absolute_error(y, y_pred, sample_weight)

customizied_fitness = make_fitness(neg_mean_absolute_error, greater_is_better=True)
customizied_fitness = make_fitness(neg_mean_absolute_error,
greater_is_better=True)

c_est_gp = SymbolicRegressor(metric=customizied_fitness, stopping_criteria=-0.000001, random_state=415,
parsimony_coefficient=0.001, verbose=0, init_method='full', init_depth=(2, 4))
c_est_gp = SymbolicRegressor(metric=customizied_fitness,
stopping_criteria=-0.000001, random_state=415,
parsimony_coefficient=0.001, verbose=0,
init_method='full', init_depth=(2, 4))
c_est_gp.fit(x_data, y_true)
c_formula = c_est_gp.__str__()

assert_equal("add(mul(X1, X1), mul(X0, X0))", c_formula, True)


Expand Down

0 comments on commit 60d0424

Please sign in to comment.