-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression.py
More file actions
120 lines (100 loc) · 4.16 KB
/
Copy pathregression.py
File metadata and controls
120 lines (100 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#========================================================================
#
# Copyright (C) 2010. Mario Rincon-Nigro.
#
# This file is a part of E-Pro.
#
# E-Pro is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Flowie is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with E-Pro. If not, see <http://www.gnu.org/licenses/>.
#
#========================================================================
import math
import sys
import random
import settings
import epro.gp.core
import epro.gp.protected
import epro.gp.tree
import epro.gp.util
import epro.gp.operator as gpop
# This is the fitness function to be used to drive
# evolution. It is the average of the sum of squared prediction
# errors. The average is taken to make proper posterior
# comparisons between training and testing errors, since
# both data sets can have different sizes
def fitness_function(tree, data_set):
tree_code = tree.getCompiledCode()
sse = 0
safe_function = epro.gp.protected.GPFunction
locals = {'add': safe_function.add,
'sub': safe_function.sub,
'mul': safe_function.mul,
'div': safe_function.div,
'pow': safe_function.pow,
'sqrt': safe_function.sqrt,
'log': safe_function.log,
'log10': safe_function.log10,
'sin': safe_function.sin,
'cos': safe_function.cos,
'tan': safe_function.tan,
'exp': safe_function.exp,
'min': safe_function.min,
'max': safe_function.max}
for item in data_set.data:
locals['x'] = item[0]
locals['y'] = item[1]
locals['z'] = item[2]
# Training error is predicted - ground truth
training_error = eval(tree_code, {}, locals) - item[3]
try:
sse += (training_error ** 2)
except Exception:
return float('inf')
return 1.0 / sse
def usage():
print "Usage: regression.py training_set testing_set seed"
if __name__ == '__main__':
if(len(sys.argv) != 4):
usage()
sys.exit(1)
training_set = epro.gp.util.CSVDataSet(sys.argv[1])
test_set = epro.gp.util.CSVDataSet(sys.argv[2])
random.seed(int(sys.argv[3])) # 23 very good
genetic_operator_set = gpop.GeneticOperatorSet(
[gpop.GeneticReproduction(),
gpop.GeneticMutation(),
gpop.GeneticCrossover()],
[20, 35, 45])
terminal_set = ['x', 'y', 'z']
internal_set = [('add', 2), ('sub', 2), ('mul', 2), ('div', 2),
('pow', 2), ('sqrt',1), ('abs', 1), ('log', 1),
('log10', 1), ('sin', 1), ('cos', 1), ('tan', 1),
('max', 2), ('min', 2)]
parameters = epro.gp.tree.TreeInitParameters(terminal_set,
internal_set, 0.05)
print "Creating population..................."
population = epro.gp.core.GPPopulation(size=settings.POPULATION_SIZE,
tree_parameters=parameters,
max_depth=settings.MAX_HEIGHT,
init_depth=settings.INITIAL_DEPTH)
evaluator = epro.gp.core.GPEvaluator(fitness_function,
training_set, test_set)
print "Evolving.............................."
best = epro.gp.core.evolution(population, genetic_operator_set,
evaluator, settings.GENERATIONS)
tr_size = len(training_set.data)
te_size = len(test_set.data)
print "+++Best individual+++"
print "\tTraining error: " + str(evaluator.evaluate(best) / tr_size)
print "\tTesting error: " + str(evaluator.testingError(best) / te_size)
print "\tLearnt function: " + str(best)