-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp1.py
225 lines (160 loc) · 3.93 KB
/
p1.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import csv
from random import randint
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import mean_squared_error
import get_mi
class feature(object):
def __init__(self):
self.delta = 0
self.pheromone = 0
class ant(object):
def __init__(self):
self.usm = []
self.ant_f = []
self.mse = 0
def getData():
temp_list = []
global num_tuples
global num_features
with open(filename, 'r') as c:
reader = csv.reader(c)
for row in reader:
ans = row.pop()
num_features = len(row)
if(len(data) == 0):
f_ans = ans
if(ans == f_ans):
result_data.append(0)
else:
result_data.append(1)
t = []
#convert from string to float
for v in row:
t.append(float(v))
data.append(t)
num_tuples = len(data)
def initFeatures():
for i in range(0, num_features):
temp = feature()
temp.delta = 0
temp.pheromone = cc
features.append(temp)
def initAnt(ants, real_ants):
global p
a = ant()
temp_selected_features = list(selected_features)
for i in range(0, m-p):
r = randint(0, len(temp_selected_features)-1)
a.ant_f.append(temp_selected_features[r])
temp_selected_features.remove(temp_selected_features[r])
#Apply USM and other measures here for the next p num_features
for i in range(0, p):
ants.append(a)
def classifyAnts(ants):
clf = LinearDiscriminantAnalysis()
# Training
for a in ants:
X = []
Y = []
for i in range(0, num_train):
train = []
for n in a.ant_f:
train.append(train_data[i][n])
X.append(train)
Y.append(result_data_train[i])
X_np = np.array(X)
Y_np = np.array(Y)
clf.fit(X_np, Y_np)
# Testing
for a in ants:
X = []
Y = []
for i in range(0, num_test):
test = []
for n in a.ant_f:
test.append(test_data[i][n])
X.append(test)
Y.append(result_data_test[i])
X_np = np.array(X)
Y_np = np.array(Y)
predict = clf.predict(X_np)
a.mse = mean_squared_error(Y_np, predict)
print a.mse
def updatePheromoneTrail(ants):
max_mse = ants[k-1].mse
common_denominator = max_mse - ants[0].mse
# Delete the previous list of attributes from which m-p features are chosen
# and fill it with the union of k best ants feature subset
del selected_features[:]
# Calculate
for i in range(0, k):
for j in range(0, len(features)):
if j in ants[i].ant_f:
features[j].delta = features[j].delta + (max_mse - ants[i].mse)/common_denominator
#Union of best k ants feature subset
if j not in selected_features:
selected_features.append(j)
# Update
for f in features:
f.pheromone = rho*f.pheromone + f.delta
f.delta = 0
def getKey(a):
return a.mse
def perform_iteration():
temp_ants = []
for i in range(0, num_ants):
initAnt(temp_ants, ants)
del ants[:]
ants = list(temp_ants)
classifyAnts(ants)
ants = sorted(ants,key=getKey)
updatePheromoneTrail(ants)
return ants[0]
##### DECLARATIONS #####
cc = 1
max_iter = 1
k = 4
pp = 8
p = 0
num_ants = 30
num_features = 0
features = []
selected_features = []
data = []
train_data = []
test_data = []
result_data = []
result_data_train = []
result_data_test = []
ants = []
f_ans = 'true'
num_tuples = 0
m = 5
split_ratio = 0.8
rho = 0.75
filename = 'CSV_Version.csv'
######////////////######
getData()
#### SPLIT TRAIN AND TEST DATA ##############
num_train = int(num_tuples*split_ratio)
num_test = num_tuples - num_train
train_data = data[:num_train]
test_data = data[num_train:]
result_data_train = result_data[:num_train]
result_data_test = result_data[num_train:]
######/////////////////////////###########
initFeatures()
mi_fc, mi_ff, cmi_ffc = getMutualInfo(filename)
if num_features <= m:
print "Number of features less than m...exiting"
exit()
for i in range(0, num_features):
selected_features.append(i)
for i in range(0, max_iter):
selected_ant = perform_iteration()
print sorted(selected_features)
# To allow only m-p features, update value of
# p from 0 to whatever
if i == 0:
p = pp