-
Notifications
You must be signed in to change notification settings - Fork 0
/
PengenalanPola.py
145 lines (128 loc) · 4.35 KB
/
PengenalanPola.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
"""
Created on Thu May 10 04:48:21 2012
@author: gofrendi
"""
#from Go_GrammaticalEvolution import Go_GrammaticalEvolution
from Go_GEFCS import Go_GEFCS
from Go_Random import Go_Random
from randomFunction import randomFunction
from abaloneData import abaloneData
ge = Go_GEFCS()
ge.random = Go_Random(randomFunction)
#ge.crossoverRate = 25
#ge.mutationRate = 45
#ge.newRate = 25
#ge.elitismRate = 5
#ge.maxEpoch = 50
#ge.populationSize = 100
#ge.maxCodon = 25
ge.startExpr = '<EXPR>'
ge.grammar = {
'<EXPR>' : [
{'become' : '(<EXPR>)<OP>(<EXPR>)', 'p' : 2},
{'become' : '<VAR>', 'p' : 8},
{'become' : '<NUM>', 'p' : 2}
],
'<OP>' : [
{'become' : '+', 'p' : 2},
{'become' : '-', 'p' : 2},
{'become' : '*', 'p' : 2},
{'become' : '/', 'p' : 2},
{'become' : '**', 'p' : 2}
],
'<VAR>' : [
{'become' : 'shucked_weight', 'p' : 2},
{'become' : 'sex', 'p' : 2},
{'become' : 'length', 'p' : 2},
{'become' : 'diameter', 'p' : 2},
{'become' : 'height', 'p' : 2},
{'become' : 'whole_weight', 'p' : 2},
{'become' : 'viscera_weight', 'p' : 2},
{'become' : 'shell_weight', 'p' : 2},
#{'become' : 'rings', 'p' : 2},
],
'<NUM>' : [
{'become' : '<DIGIT>.<DIGIT>', 'p' : 1},
{'become' : '<DIGIT>', 'p' : 9}
],
'<DIGIT>' : [
{'become' : '<DIGIT><DIGIT>', 'p' : 1},
{'become' : '0', 'p' : 2},
{'become' : '1', 'p' : 2},
{'become' : '2', 'p' : 2},
{'become' : '3', 'p' : 2},
{'become' : '4', 'p' : 2},
{'become' : '5', 'p' : 2},
{'become' : '6', 'p' : 2},
{'become' : '7', 'p' : 2},
{'become' : '8', 'p' : 2},
{'become' : '9', 'p' : 2}
]
}
trainingSet = {
'header' : ['sex', 'length', 'diameter', 'height', 'whole_weight', 'shucked_weight', 'viscera_weight', 'shell_weight', 'rings'],
'target' : 'rings+1.5',
'data' : abaloneData
}
ge.trainingSet = trainingSet
ge.train()
#ge.printAllPhenotype()
#good features should have correlation with the output (done),
#good features should not be correlated each other
bestPhenotype = ge.getBestPhenotype(4, 0)
for phenotype in bestPhenotype:
print(phenotype)
all_extracted_features = []
all_original_features = []
all_targets = []
trainingHeader = trainingSet['header']
for trainingData in trainingSet['data']:
sandbox = {}
extracted_inputs = []
original_inputs = []
targets = []
for i in xrange(len(trainingHeader)):
exec(trainingHeader[i]+'='+str(float(trainingData[i]))) in sandbox
original_inputs.append(float(sandbox[trainingHeader[i]]))
for i in xrange(len(bestPhenotype)):
exec('_result = '+str(bestPhenotype[i])) in sandbox
extracted_inputs.append(float(sandbox['_result']))
exec('_result = '+str(trainingSet['target'])) in sandbox
targets = float(sandbox['_result'])
all_extracted_features.append(extracted_inputs)
all_original_features.append(original_inputs)
all_targets.append(targets)
#print all_extracted_features
#print all_targets
from sklearn import svm
#Extracted features========================
clf = svm.SVC(kernel='linear', scale_C=True)
clf.fit(all_extracted_features, all_targets)
all_predicts = clf.predict(all_extracted_features)
right = 0
wrong = 0
mse = 0.0
for i in xrange(len(all_targets)):
mse += pow((all_targets[i]-all_predicts[i]), 2)
if abs(all_targets[i]-all_predicts[i])>1:
wrong +=1
else:
right +=1
mse = pow(mse, 0.5)/len(all_targets)
print('By using extacted features, right : %d, wrong : %d, mse : %5.3f' %(right, wrong, mse))
#Original features========================
clf = svm.SVC(kernel='linear', scale_C=True)
clf.fit(all_original_features, all_targets)
all_predicts = clf.predict(all_original_features)
right = 0
wrong = 0
mse = 0.0
for i in xrange(len(all_targets)):
mse += pow((all_targets[i]-all_predicts[i]), 2)
if abs(all_targets[i]-all_predicts[i])>1:
wrong +=1
else:
right +=1
mse = pow(mse, 0.5)/len(all_targets)
print('By using original features, right : %d, wrong : %d, mse : %5.3f' %(right, wrong, mse))