-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
295 lines (250 loc) · 12 KB
/
main.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from scipy.spatial import distance #for euclidian distance
from pprint import pprint
def intersect(dataSet, k):
if not dataSet: #if data set is empty then doesn't intersect
return False
for x in dataSet:
if(x == k):
return True
return False
def leave_one_out_cross_validation(dataSet, current_set_of_features, k, algorithm):
column_array = []
#current_set_of_features = [1,3,4,5,6,7,8,9,10]
#current_set_of_features = [2,4,5]
#column_arry[[f1],[f2]...]
#if current set isn't empty then add each corresponding column
#onto column_array
index = 0
if(algorithm == 2):
for i, features in enumerate(current_set_of_features):
if features == k:
index = i
if current_set_of_features:
for i in range(len(current_set_of_features)):
column_temp = []
if (i != index) or (algorithm==1) or (algorithm==-1):
for j in range(len(dataSet)):
column_temp.append(float(dataSet[j][current_set_of_features[i]]))
column_array.append(column_temp)
if(algorithm == 1):
#add feature k onto column_array
column_temp2 = []
for x in range(len(dataSet)):
column_temp2.append(float(dataSet[x][k]))
column_array.append(column_temp2)
points = []
#form points from the row i.g. "[x,y,z,...,n]"
#append to points; list of point
for i in range(len(column_array[0])):
points_temp = []
for j in range(len(column_array)):
points_temp.append(float(column_array[j][i]))
points.append(points_temp)
class_list = [] #list containing if class matches or not
index_i = -1;
index_j = -1;
#compare each points with one another
#find nearest neighbor for every point then check
#if they're the same class/if it's the correct class
for i in range(len(points)):
shortest_distance = float('inf')
for j in range(len(points)):
temp_dst = distance.euclidean(points[i],points[j])
if(shortest_distance > temp_dst and j != i):
shortest_distance = temp_dst
index_i = i
index_j = j
#if matches then true, else false1
if(dataSet[index_j][0] == dataSet[index_i][0]):
class_list.append(True)
else:
class_list.append(False)
cnt = 0.0
for i in range(len(class_list)):
if(class_list[i]):
cnt += 1
return cnt
def hehe(dataSet, current_set_of_features, k, counter):
column_array = []
#convert numbers to columns
if current_set_of_features:
for i in range(len(current_set_of_features)):
column_temp = []
for j in range(len(dataSet)):
column_temp.append(float(dataSet[j][current_set_of_features[i]]))
column_array.append(column_temp)
#add feature k onto column_array
column_temp2 = []
for x in range(len(dataSet)):
column_temp2.append(float(dataSet[x][k]))
column_array.append(column_temp2)
points = []
#form points from the row i.g. "[x,y,z,...,n]"
#append to points; list of point
for i in range(len(column_array[0])):
points_temp = []
for j in range(len(column_array)):
points_temp.append(float(column_array[j][i]))
points.append(points_temp)
class_list = [] #list containing if class matches or not
index_i = -1;
index_j = -1;
sum_False = 0;
#compare each points with one another
#find nearest neighbor for every point then check
#if they're the same class/if it's the correct class
for i in range(len(column_array[0])):
shortest_distance = float('inf')
for j in range(len(column_array[0])):
temp_dst = distance.euclidean(points[i],points[j])
if(shortest_distance > temp_dst and j != i):
shortest_distance = temp_dst
index_i = i
index_j = j
#if matches then true, else false1
if(dataSet[index_j][0] == dataSet[index_i][0]):
class_list.append(True)
else:
class_list.append(False)
sum_False += 1
#if wrong for more than previous instance, just stop and return 0
if(sum_False > counter):
return 0
#count num of correct instances
cnt = 0.0
for i in range(len(class_list)):
if(class_list[i]):
cnt += 1
return cnt
#read in from file "data.txt" and store content into 2d list
def populateDataSet(fileName):
file = open(fileName, "r") #open file
temp = file.readlines() #read line by line
temp1 = [] #what 2d list with numbers
for line in temp: #split each line and store into temp1
temp1.append(line.split())
return temp1
def forwardSelection(dataSet):
current_set_of_features = []; #current feature list
best_best_accuracy = 0;
current_best_features = []; #highest acc set of features; display at end
for i in range(1,len(dataSet[0])): #traverse through number of features
print "On Level " + str(i) + " of the search tree:"
feature_to_add_on_this_level = -1 #feature w/ highest acc in level
best_accuracy_so_far = 0 #level accuracy
for j in range(1,len(dataSet[i])):
if not intersect(current_set_of_features, j):
accuracy = leave_one_out_cross_validation(dataSet, current_set_of_features, j, 1)
if not current_set_of_features:
print "---Using feature(s) {" + str(j) + "} accuracy is " + str(accuracy) + "%"
else:
print "---Using feature(s) {" + str(j) + ", " + "".join(str(current_set_of_features)) + "} accuracy is " + str(accuracy) + "%"
if(accuracy > best_accuracy_so_far):
best_accuracy_so_far = accuracy
feature_to_add_on_this_level = j
if(best_accuracy_so_far > best_best_accuracy):
best_best_accuracy = best_accuracy_so_far
current_best_features.append(feature_to_add_on_this_level)
else:
print "\n(Warning, Accuracy has decreased! Continuing search in case of local maxima)"
if(feature_to_add_on_this_level != -1):
current_set_of_features.append(feature_to_add_on_this_level)
print "Feature set " + "".join(str(current_set_of_features)) + " was best with an accuracy of " + str(best_accuracy_so_far) + "%\n"
print "Best feature(s): " + "".join(str(current_best_features)) + ", with an accuracy of " + str(best_best_accuracy) + "%"
def backwardElimination(dataSet):
#backwards elimination starst with a full set of features
current_set_of_features = [];
for i in range(1, len(dataSet[0])):
current_set_of_features.append(i)
best_best_accuracy = 0;
current_best_features = [];
for i in range(1,len(dataSet[0])-1): #traverse number of features
feature_to_add_on_this_level = -1
best_accuracy_so_far = 0
if(len(current_best_features) != 1): #if there is only 1 feature left, we don't want to remove
print "On Level " + str(i) + " of the search tree:"
for j in range(1,len(dataSet[0])): #traverse number of features
if intersect(current_set_of_features,j): #choose only features still in the list to remove
accuracy = leave_one_out_cross_validation(dataSet, current_set_of_features, j, 2)
print "---Removing feature {" + str(j) + "}, accuracy is " + str(accuracy) + "%"
if(accuracy >= best_accuracy_so_far): #if accuracy increases than lvl acc
best_accuracy_so_far = accuracy #replaced level acc
feature_to_add_on_this_level = j #store index
if(feature_to_add_on_this_level != -1):
index = 0
for y, features in enumerate(current_set_of_features):
if features == feature_to_add_on_this_level:
index = y
current_set_of_features.pop(index)
print "Feature set " + "".join(str(current_set_of_features)) + " was best with an accuracy of " + str(best_accuracy_so_far) + "%\n"
if(best_accuracy_so_far > best_best_accuracy): #check if level acc is better than overall acc
best_best_accuracy = best_accuracy_so_far #replace overall acc
current_best_features = [features for features in current_set_of_features] #deep copy
#store the current set bc it's the best so far
elif(best_accuracy_so_far < best_best_accuracy and len(current_best_features) != 1):
print "(Warning, Accuracy has decreased! Continuing search in case of local maxima)\n"
print "Best features: " + "".join(str(current_best_features)) + ", with an accuracy of " + str(best_best_accuracy) + "%"
def myAlgorithm(dataSet):
current_set_of_features = []; #current feature list
best_best_accuracy = 0;
current_best_features = []; #highest acc set of features; display at end
for i in range(1,len(dataSet[0])): #traverse through number of features
print "On Level " + str(i) + " of the search tree:"
feature_to_add_on_this_level = -1 #feature w/ highest acc in level
best_accuracy_so_far = 0 #level accuracy
num_of_wrong_on_level = float("inf")
num_of_wrong_overall = float("inf")
for j in range(1,len(dataSet[i])):
if not intersect(current_set_of_features, j):
if (num_of_wrong_on_level >= num_of_wrong_overall):
accuracy = hehe(dataSet, current_set_of_features, j, num_of_wrong_on_level)
else:
accuracy = hehe(dataSet, current_set_of_features, j, num_of_wrong_overall)
if (accuracy != 0):
num_of_wrong_on_level = len(dataSet) - accuracy
else:
num_of_wrong_on_level = len(dataSet)- best_accuracy_so_far
if (num_of_wrong_on_level < num_of_wrong_overall):
num_of_wrong_overall = num_of_wrong_on_level
if not current_set_of_features:
print "---Using feature(s) {" + str(j) + "} accuracy is " + str(accuracy) + "%"
else:
print "---Using feature(s) {" + str(j) + ", " + "".join(str(current_set_of_features)) + "} accuracy is " + str(accuracy) + "%"
if(accuracy > best_accuracy_so_far):
best_accuracy_so_far = accuracy
feature_to_add_on_this_level = j
if(best_accuracy_so_far > best_best_accuracy):
best_best_accuracy = best_accuracy_so_far
current_best_features.append(feature_to_add_on_this_level)
else:
print "\n(Warning, Accuracy has decreased! Continuing search in case of local maxima)"
if(feature_to_add_on_this_level != -1):
current_set_of_features.append(feature_to_add_on_this_level)
print "Feature set " + "".join(str(current_set_of_features)) + " was best with an accuracy of " + str(best_accuracy_so_far) + "%\n"
print "Best feature(s): " + "".join(str(current_best_features)) + ", with an accuracy of " + str(best_best_accuracy) + "%"
#program starts here
print "Welcome to Nam Nguyen's Feature Selection Algorithm."
fileName = raw_input("Type in the name of the file to test: ")
dataSet = populateDataSet(fileName)
all_features = [];
for i in range(1, len(dataSet[0])):
all_features.append(i)
print """Choose which algorithm to run:
1) Forward Selection
2) Backward Elimination
3) Nam's Special Algorithm"""
algorithm_input = raw_input("I want to run algorithm #")
if(algorithm_input == "1"):
print "\nThis dataSet has: " + str(len(dataSet[0]) - 1) + " features (not including class attribute), with " + str(len(dataSet)) + " instances.\n"
print "Running nearest neighbor with all " + str(len(dataSet[0]) - 1) + " features, using \"leave-one-out\" evaluation, I get an accuracy of " + str(leave_one_out_cross_validation(dataSet, all_features, -1, -1)) + "%\n"
forwardSelection(dataSet)
elif(algorithm_input == "2"):
print "\nThis dataSet has: " + str(len(dataSet[0]) - 1) + " features (not including class attribute), with " + str(len(dataSet)) + " instances.\n"
print "Running nearest neighbor with all " + str(len(dataSet[0]) - 1) + " features, using \"leave-one-out\" evaluation, I get an accuracy of " + str(leave_one_out_cross_validation(dataSet, all_features, -1, -1)) + "%\n"
backwardElimination(dataSet)
elif(algorithm_input == "3"):
print "\nThis dataSet has: " + str(len(dataSet[0]) - 1) + " features (not including class attribute), with " + str(len(dataSet)) + " instances.\n"
print "Running nearest neighbor with all " + str(len(dataSet[0]) - 1) + " features, using \"leave-one-out\" evaluation, I get an accuracy of " + str(leave_one_out_cross_validation(dataSet, all_features, -1, -1)) + "%\n"
myAlgorithm(dataSet)
else:
print "Dude...just choose 1, 2, or 3 like a normal person"