Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for parents pressure for selection #22

Merged
merged 3 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions few/few.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, population_size=50, generations=100,
random_state=np.random.randint(9999999), 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 @@ -91,6 +91,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 = random_state
Expand Down Expand Up @@ -820,6 +821,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 @@ -947,7 +951,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 @@ -46,6 +46,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