Skip to content

Commit

Permalink
Merge pull request #22 from lacava/issues_fix
Browse files Browse the repository at this point in the history
changes for parents pressure for selection
  • Loading branch information
lacava committed Jun 26, 2017
2 parents f83a663 + d5bc3ab commit b6310de
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
8 changes: 6 additions & 2 deletions few/few.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, population_size=50, generations=100,
random_state=None, verbosity=0,
scoring_function=None, disable_update_check=False,
elitism=True, boolean = False,classification=False,clean=False,
track_diversity=False,mdr=False,otype='f',c=True):
track_diversity=False,mdr=False,otype='f',c=True, weight_parents=False):
# sets up GP.

# Save params to be recalled later by get_params()
Expand Down Expand Up @@ -93,6 +93,7 @@ def __init__(self, population_size=50, generations=100,
self.fit_choice = fit_choice
self.op_weight = op_weight
self.max_stall = max_stall
self.weight_parents = weight_parents
self.seed_with_ml = seed_with_ml
self.erc = erc
self.random_state = check_random_state(random_state)
Expand Down Expand Up @@ -754,6 +755,9 @@ def main():
type=positive_integer, help='If model CV does not '
'improve for this many generations, end optimization.')

parser.add_argument('--weight_parents', action='store_true',dest='WEIGHT_PARENTS',default=False,
help='Feature importance determines parent pressure for selection.')

parser.add_argument('-sel', action='store', dest='SEL',
default='epsilon_lexicase',
choices = ['tournament','lexicase','epsilon_lexicase',
Expand Down Expand Up @@ -881,7 +885,7 @@ def main():
fit_choice = args.FIT_CHOICE,boolean=args.BOOLEAN,
classification=args.CLASSIFICATION,clean = args.CLEAN,
track_diversity=args.TRACK_DIVERSITY,mdr=args.MDR,
otype=args.OTYPE,c=args.c)
otype=args.OTYPE,c=args.c, weight_parents = args.WEIGHT_PARENTS)

learner.fit(training_features, training_labels)
# pdb.set_trace()
Expand Down
24 changes: 24 additions & 0 deletions few/tests/test_few.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ def test_few_fit_shapes():
"test r2:",r2_score(boston.target[300:],yhat_test))
assert yhat_test.shape == boston.target[300:].shape

def test_few_with_parents_weight():
"""test_few.py: few performs without error with parent pressure for selection"""
np.random.seed(1006987)
boston = load_boston()
d = np.column_stack((boston.data,boston.target))
np.random.shuffle(d)
features = d[:,0:-1]
target = d[:,-1]

print("feature shape:",boston.data.shape)

learner = FEW(generations=1, population_size=5,
mutation_rate=1, crossover_rate=1,
ml = LassoLarsCV(), min_depth = 1, max_depth = 3,
sel = 'tournament', fit_choice = 'r2',tourn_size = 2, random_state=0, verbosity=0,
disable_update_check=False, weight_parents=True)

learner.fit(features[:300], target[:300])
few_score = learner.score(features[:300], target[:300])
test_score = learner.score(features[300:],target[300:])

print("few score:",few_score)
print("few test score:",test_score)


def test_few_at_least_as_good_as_default():
"""test_few.py: few performs at least as well as the default ML """
Expand Down
14 changes: 12 additions & 2 deletions few/variation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ def variation(self,parents):
if type(self.ml).__name__ != 'SVC' and type(self.ml).__name__ != 'SVR': # this is needed because svm has a bug that throws valueerror on attribute check
if hasattr(self.ml,'coef_'):
# for l1 regularization, filter individuals with 0 coefficients
offspring = copy.deepcopy(list(x for i,x in zip(self.ml.coef_, self.valid(parents)) if (i != 0).any()))
if self.weight_parents:
weights = abs(self.ml.coef_)
weights = weights/sum(weights)
offspring = copy.deepcopy(list(np.random.choice(self.valid(parents), self.population_size, p=weights)))
else:
offspring = copy.deepcopy(list(x for i,x in zip(self.ml.coef_, self.valid(parents)) if (i != 0).any()))
elif hasattr(self.ml,'feature_importances_'):
# for tree methods, filter our individuals with 0 feature importance
offspring = copy.deepcopy(list(x for i,x in zip(self.ml.feature_importances_, self.valid(parents)) if i != 0))
if self.weight_parents:
weights = self.ml.feature_importances_
weights = weights/sum(weights)
offspring = copy.deepcopy(list(np.random.choice(self.valid(parents), self.population_size, p=weights)))
else:
offspring = copy.deepcopy(list(x for i,x in zip(self.ml.feature_importances_, self.valid(parents)) if i != 0))
else:
offspring = copy.deepcopy(self.valid(parents))
else:
Expand Down

0 comments on commit b6310de

Please sign in to comment.