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

Add the new feature of customized initial population #1352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions tests/test_custom_iniPop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from tpot import TPOTClassifier
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

digits = load_digits()
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target,
train_size=0.75, test_size=0.25, random_state=42)

individual_str1 = 'MultinomialNB(input_matrix, MultinomialNB__alpha=0.1, MultinomialNB__fit_prior=True)'
individual_str2 = 'GaussianNB(DecisionTreeClassifier(input_matrix, DecisionTreeClassifier__criterion=entropy, DecisionTreeClassifier__max_depth=4, DecisionTreeClassifier__min_samples_leaf=17, DecisionTreeClassifier__min_samples_split=13))'
individual_str3 = 'GaussianNB(SelectFwe(CombineDFs(input_matrix, ZeroCount(input_matrix))))'

est = TPOTClassifier(generations=3, population_size=5, verbosity=2, random_state=42, config_dict=None,
customized_initial_population=[individual_str1, individual_str2, individual_str3],
)
est.fit(X_train, y_train)
print(est.score(X_test, y_test))
25 changes: 24 additions & 1 deletion tpot/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(
verbosity=0,
disable_update_check=False,
log_file=None,
customized_initial_population=None,
):
"""Set up the genetic programming algorithm for pipeline optimization.

Expand Down Expand Up @@ -310,6 +311,7 @@ def __init__(
self.disable_update_check = disable_update_check
self.random_state = random_state
self.log_file = log_file
self.customized_initial_population = customized_initial_population

def _setup_template(self, template):
self.template = template
Expand Down Expand Up @@ -557,6 +559,9 @@ def _setup_toolbox(self):
self._toolbox.register(
"population", tools.initRepeat, list, self._toolbox.individual
)
self._toolbox.register(
"customized_population", self._initPopulation_customized, customized_initial_population = self.customized_initial_population
)
self._toolbox.register("compile", self._compile_to_sklearn)
self._toolbox.register("select", tools.selNSGA2)
self._toolbox.register("mate", self._mate_operator)
Expand Down Expand Up @@ -764,7 +769,10 @@ def fit(self, features, target, sample_weight=None, groups=None):

# assign population, self._pop can only be not None if warm_start is enabled
if not self._pop:
self._pop = self._toolbox.population(n=self.population_size)
if not self.customized_initial_population:
self._pop = self._toolbox.population(n=self.population_size) # generate initial population by default
else:
self._pop = self._toolbox.customized_population(customized_initial_population=self.customized_initial_population) # generate initial population by custom

def pareto_eq(ind1, ind2):
"""Determine whether two individuals are equal on the Pareto front.
Expand Down Expand Up @@ -2026,6 +2034,21 @@ def _generate(self, pset, min_, max_, condition, type_=None):
stack.append((depth + 1, arg))
return expr

def _initPopulation_customized(self, customized_initial_population):
iniPop = [] # a list of <class 'deap.creator.Individual'> pipelines
for individual_str in customized_initial_population:
individual = creator.Individual.from_string(individual_str, self._pset) # converting individual_str to individual
iniPop.append(individual)
"check if #customized initial pipelines <= #population"
if len(iniPop) <= self.population_size:
for _ in range(self.population_size - len(iniPop)):
individual_rand = self._toolbox.individual()
iniPop.append(individual_rand)
print(len(customized_initial_population), "customized pipelines +", self.population_size - len(customized_initial_population), "randomized pipelines as initial population.")
else:
raise Exception("the number of customized initial pipelines > the number of population size!")
return iniPop


@property
def classes_(self):
Expand Down