Skip to content

Commit f21fbea

Browse files
committed
get n_pop and n_gen from command-line
1 parent 7464fed commit f21fbea

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

gaFeatureSelection.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def getFitness(individual, X, y):
3737
return(0,)
3838

3939

40-
def geneticAlgorithm(X, y):
40+
def geneticAlgorithm(X, y, n_population, n_generation):
4141
"""
4242
Deap global variables
4343
Initialize variables to use eaSimple
@@ -59,8 +59,6 @@ def geneticAlgorithm(X, y):
5959
toolbox.register("select", tools.selTournament, tournsize=3)
6060

6161
# initialize parameters
62-
n_population = 10
63-
n_generation = 2
6462
pop = toolbox.population(n=n_population)
6563
hof = tools.HallOfFame(n_population * n_generation)
6664
stats = tools.Statistics(lambda ind: ind.fitness.values)
@@ -92,9 +90,24 @@ def bestIndividual(hof, X, y):
9290
return _individual.fitness.values, _individual, _individualHeader
9391

9492

93+
def getArguments():
94+
"""
95+
Get argumments from command-line
96+
If pass only dataframe path, pop and gen will be default
97+
"""
98+
dfPath = sys.argv[1]
99+
if(len(sys.argv) == 4):
100+
pop = int(sys.argv[2])
101+
gen = int(sys.argv[3])
102+
else:
103+
pop = 10
104+
gen = 2
105+
return dfPath, pop, gen
106+
107+
95108
if __name__ == '__main__':
96-
# get dataframe path from command-line argument
97-
dataframePath = sys.argv[1]
109+
# get dataframe path, population number and generation number from command-line argument
110+
dataframePath, n_pop, n_gen = getArguments()
98111
# read dataframe from csv
99112
df = pd.read_csv(dataframePath, sep=',')
100113

@@ -110,7 +123,7 @@ def bestIndividual(hof, X, y):
110123
str(getFitness(individual, X, y)) + "\n")
111124

112125
# apply genetic algorithm
113-
hof = geneticAlgorithm(X, y)
126+
hof = geneticAlgorithm(X, y, n_pop, n_gen)
114127

115128
# select the best individual
116129
accuracy, individual, header = bestIndividual(hof, X, y)

0 commit comments

Comments
 (0)