-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
427 lines (345 loc) · 16.8 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import numpy as np
import math
import random
import logging
import time
import argparse
from pathlib import Path
from collections import Counter
# Cat to prevent bugs
# _
# \`*-.
# ) _`-.
# . : `. .
# : _ ' \
# ; *` _. `*-._
# `-.-' `-.
# ; ` `.
# :. . \
# . \ . : .-' .
# ' `+.; ; ' :
# : ' | ; ;-.
# ; ' : :`-: _.`* ;
# [bug] .*' / .*' ; .*`- +' `*'
# `*-* `*-* `*-*'
SMALL_DATA = Path("./datasets/small-test-dataset.txt")
BIG_DATA = Path("./datasets/large-test-dataset.txt")
TITANIC = Path("./datasets/titanic.txt")
# Titanic Feature Key
#Feature 1: Passenger Ticket Class
#Feature 2: Passenger Sex
#Feature 3: Passenger Age in Years
#Feature 4: Number of Passenger's Siblings Aboard the Titanic
#Feature 5: Number of Passenger's Parents/Children Aboard the Titanic
#Feature 6: Passenger Fare (Ticket Cost)
class Data:
labels = np.array
features = np.array
featureList = np.array
normalizationMethod = None
dataset : Path
def __init__ (self, normalizeMethod : int = 0):
self.normalizationMethod = normalizeMethod
def loadTestData(self, testSet=SMALL_DATA):
self.dataset = testSet
logging.info(f"Loading {testSet}...")
data = np.loadtxt(testSet)
# Extract labels (first column)
self.labels = data[:, 0].astype(int)
# Extract features (all columns except first)
features_to_normalize = data[:, 1:]
#Normalization Method
match self.normalizationMethod:
case "min-max": #Min-Max
logging.info(f"Normalization Method: Min-Max")
min_vals = np.min(features_to_normalize, axis=0)
max_vals = np.max(features_to_normalize, axis=0)
# Min-max normalization
features_to_normalize = (features_to_normalize - min_vals) / (max_vals - min_vals)
case "std-norm": #Standard Normal
logging.info(f"Normalization Method: Standard-Normal")
means = np.mean(features_to_normalize, axis=0)
stds = np.std(features_to_normalize, axis=0)
# Normalize features using z-score normalization
features_to_normalize = (features_to_normalize - means) / stds
case "numpy":
logging.info(f"Normalization Method: Numpy Default")
np.linalg.norm(features_to_normalize)
case "none": #NONE
logging.info(f"Normalization Method: NONE")
self.features = features_to_normalize
logging.info("Features normalized")
logging.info(f"Data Successfully Loaded into Matrix of Size {data.shape}")
def loadFeatureList(self, featureList):
self.featureList = np.array(featureList)
logging.debug(f"Feature list loaded: {featureList}")
class Classifier: # Calculates distance between every point for NN
data = Data()
kNN = 1
def train(self,data, kNN = 1):
self.data = data
logging.debug(f"Classifier Training Data Loaded!")
self.kNN = kNN
logging.info(f"Set Nearest Neighbor K={self.kNN}")
def test(self, testIndex: int) -> int:
# left shift indexes (1 -> 0)
feature_indices = [i-1 for i in self.data.featureList]
# isolate test row
test_point = self.data.features[testIndex, feature_indices]
# remove our test index from features and label array
remaining_features = np.delete(self.data.features[:, feature_indices], testIndex, axis=0)
remaining_labels = np.delete(self.data.labels, testIndex)
#https://jaykmody.com/blog/distance-matrices-with-numpy/
# array of euclidan dists only wrt selected features
# for each row, subtract test point features from current features -> square it -> sum it -> sqrt it
distances = np.sqrt(np.sum((remaining_features - test_point)**2, axis=1))
# new array of indexes corrisponding to distance list in sorted order
# slice the kNN smallest distance indexs
nearest_indices = np.argsort(distances)[:self.kNN]
# tally the winner
voters = Counter()
for idx in nearest_indices:
label = remaining_labels[idx]
voters[label] += 1
result = voters.most_common(1)[0][0]
return result
class Validator: #Computes classifier's accuracy
def __init__(self):
pass
def evaluate(self, data: Data, classifier: Classifier, featureList=None) -> float:
correct = 0
total = data.features.shape[0]
if featureList:
data.loadFeatureList(featureList)
timeStart = time.perf_counter_ns()
# loop through every row -> guess answer when leaving out that row
for R in range(total):
predicted = classifier.test(R)
actual = data.labels[R]
if predicted == actual:
correct += 1
logging.debug(f"Instance {R}: Predicted={predicted}, Actual={actual}")
timeEnd = time.perf_counter_ns()
accuracy = correct / total
logging.info(f"Features: {featureList} Accuracy: {round(accuracy,4)} Time: {round((timeEnd - timeStart)*10**(-9), 8)}s")
return accuracy
class FeatureSearch:
featureList = []
vally : Validator
dadi: Data
classi : Classifier
def __init__(self, vally : Validator, data : Data, classi : Classifier):
self.featureList = list(range(1,data.features.shape[1]))
self.vally = vally
self.dadi = data
self.classi = classi
def evaluate(self):
return random.randint(1,100) #stubbed
def forwardSelection(self) -> list:
n = len(self.featureList)
parentAccuracy = -math.inf
currentFeatures = set()
depth = 0
print(Printer.searchStartForward)
timeStart = time.perf_counter_ns()
while depth < n:
bestChildAccuracy = (-math.inf, None) # (EVAL_SCORE, FEATURE_INDEX_TO_ADD)
for i in range(n):
if self.featureList[i] in currentFeatures: continue
currentFeatures.add(self.featureList[i])
eval = self.vally.evaluate(self.dadi, self.classi, list(currentFeatures))
logging.debug(f"{currentFeatures} Evaluated at {eval}")
if eval > bestChildAccuracy[0]:
bestChildAccuracy = (eval,i)
currentFeatures.remove(self.featureList[i]) #backtrack
if bestChildAccuracy[0] < parentAccuracy: # No better options dont add -> exit
timeEnd = time.perf_counter_ns()
logging.info(f"Time: {round((timeEnd - timeStart)*10**(-9), 8)}m")
print(Printer.searchQuit)
break
featureChanged = self.featureList[bestChildAccuracy[1]]
currentFeatures.add(featureChanged) # Add back best branch
Printer.printFeatureChange(featureChanged,currentFeatures,bestChildAccuracy[0],True)
parentAccuracy = bestChildAccuracy[0]
depth += 1
Printer.printFeatureListSelected(currentFeatures,parentAccuracy)
return list(currentFeatures)
def backwardElimination(self) -> list:
n = len(self.featureList)
global_best_accuracy = -math.inf
global_best_features = set()
current_features = set(self.featureList)
depth = 1
logging.info(Printer.searchStartBackward)
timeStart = time.perf_counter_ns()
while depth < n:
bestChildAccuracy = (-math.inf, 0) # (eval, index of that item)
# Try removing each feature
for i in range(n):
if self.featureList[i] not in current_features:
continue
current_features.remove(self.featureList[i])
eval = self.vally.evaluate(self.dadi, self.classi, list(current_features))
logging.debug(f"Evaluated {current_features} at {eval}")
# Update best child if current evaluation is better
if eval > bestChildAccuracy[0]:
bestChildAccuracy = (eval, i)
# Update global best if we found a better solution
if eval > global_best_accuracy:
global_best_accuracy = eval
global_best_features = set(current_features)
current_features.add(self.featureList[i])
# no improvement, but we can try to search deeper
if bestChildAccuracy[0] < global_best_accuracy and depth < n-1:
# still remove the worst feature, pray we escape local extrema
available_features = list(current_features)
if available_features: # Make sure we have features to remove
feature_to_remove = random.choice(available_features)
current_features.remove(feature_to_remove)
logging.info(f"Attempting to escape local maximum by removing feature {feature_to_remove}")
else:
# Remove the feature that gave the worst result
feature_changed = self.featureList[bestChildAccuracy[1]]
current_features.remove(feature_changed)
Printer.printFeatureChange(feature_changed, current_features, bestChildAccuracy[0], False)
depth += 1
timeEnd = time.perf_counter_ns()
logging.info(f"Time: {round((timeEnd - timeStart)*10**(-9), 8)}s")
# Return to the globally best feature set found
Printer.printFeatureListSelected(global_best_features, global_best_accuracy)
return list(global_best_features)
def simulatedAnnealing(self) -> list:
n = len(self.featureList)
current_features = set(range(1, n+1)) #all features
current_accuracy = self.vally.evaluate(self.dadi, self.classi, list(current_features))
best_features = set(current_features)
best_accuracy = current_accuracy
visited = set()
# Settings
initial_temp = 1.0
final_temp = 0.01
alpha = 0.99 # cooling rate
iterations_per_temp = 20
current_temp = initial_temp
logging.info("Starting Simulated Annealing Search...")
timeStart = time.perf_counter_ns()
while current_temp > final_temp:
for _ in range(iterations_per_temp):
neighbor_features = set(current_features)
if random.random() < 0.5 and len(neighbor_features) > 1: # Remove feature
feature_to_remove = random.choice(list(neighbor_features))
neighbor_features.remove(feature_to_remove)
else: # Add feature
available_features = set(range(1, n+1)) - neighbor_features
if available_features:
feature_to_add = random.choice(list(available_features))
neighbor_features.add(feature_to_add)
#Don't revisit the same set
if frozenset(neighbor_features) in visited: continue
# Evaluate neighbor solution
neighbor_accuracy = self.vally.evaluate(self.dadi, self.classi, list(neighbor_features))
#acceptance probability
delta = neighbor_accuracy - current_accuracy
acceptance_probability = min(1.0, math.exp(delta / current_temp))
# accept or reject random selection
if delta > 0 or random.random() < acceptance_probability:
current_features = neighbor_features
current_accuracy = neighbor_accuracy
visited.add(frozenset(current_features))
# Maybe update best solution!
if current_accuracy > best_accuracy:
best_accuracy = current_accuracy
best_features = set(current_features)
logging.info(f"New best solution found: {best_features} with accuracy {best_accuracy}")
logging.debug(f"Temperature: {current_temp:.4f}, Current Accuracy: {current_accuracy:.4f}")
# cooling
current_temp *= alpha
timeEnd = time.perf_counter_ns()
logging.info(f"Time: {round((timeEnd - timeStart)*10**(-9), 8)}s")
Printer.printFeatureListSelected(best_features, best_accuracy)
return list(best_features)
class Printer:
mainWelcome : str = "\nWelcome to SZIMM011 and LADAM020's Project 2!"
searchStartForward : str = "Starting Forward Selection Search... "
searchStartBackward : str = "Starting Backward Elimination Search... "
searchQuit : str = "All Children Result in Lower Accuracy, Terminating Search..."
feetAlgPrompt : str ="""Type the number of the algorithm you want to run
1) Forward Selection
2) Backward Elimination
3) Simulated Annealing
Choice: """
datAlgPrompt : str ="""Type the number cooresponding to the data you want
1) Big data
2) Small data
3) Titanic data
Choice: """
@staticmethod
def featureAlgPrompt(feet: FeatureSearch) -> list:
inny = input(Printer.feetAlgPrompt)
if inny == '1':
return feet.forwardSelection()
elif inny == '2':
return feet.backwardElimination()
else:
return feet.simulatedAnnealing()
@staticmethod
def dataAlgPrompt() -> Path:
datPick = input(Printer.datAlgPrompt)
if datPick == '1':
return BIG_DATA
elif datPick == '2':
return SMALL_DATA
else:
return TITANIC
@staticmethod
def printFeatureListSelected(Currentfeatures,accuracy):
print(f"Best Feature Set Found: {Currentfeatures}")
print(f"Accuracy: {accuracy}\n")
@staticmethod
def printFeatureChange(featureChanged,currentFeatures,accuracy,add=True):
if add: logging.info(f"Adding Best Feature: {featureChanged}" )
else: logging.info(f"Removing Worst Feature: {featureChanged}" )
logging.info(f"New Feature Set: {currentFeatures} Accuracy: {accuracy}")
#MAIN
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog='Feature Selection with Nearest Neighbor Classifier',
description='Given a dataset with a binary label as the first column, this program will attempt to classify the data. Using a leave one out validator it will attempt to find the most accurate feature set for classification.',
epilog='Text at the bottom of help')
parser.add_argument('--customdata', '-d', type=Path, default=None, help='Provide the path to a custom dataset.')
parser.add_argument('--testdata', default='titanic',choices=["bigdata","smalldata","titanic"],help="Use a provided test/sample dataset")
parser.add_argument('--search', '-s',choices=["forward","backward","simulated-annealing"],default='forward',help='Pick the feature search method\n 1.[forward] Selection\n2.[backward] Elminiation\n3.simulated-annealing')
parser.add_argument('--debug', type=bool, default=False, help="Display debug info during run" )
parser.add_argument('--NN', '-k', type=int, default=3, help="Set the k value for nearest-neighbor. How many neighbors should be considered?")
parser.add_argument("--normalization",'-norm',action="store",default='min-max', choices=['min-max','std-normal','numpy','none'], help="Set the method of data normalization\nIf your data is already normalize, use 'none'")
print(Printer.mainWelcome)
args=parser.parse_args()
if args.debug:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
else:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
#Data
dadi = Data(args.normalization)
if args.customdata:
dataPath = args.customdata
else:
match args.testdata:
case "smalldata":
dataPath=SMALL_DATA
case "bigdata":
dataPath=BIG_DATA
case _:
dataPath=TITANIC
dadi.loadTestData(dataPath)
classi=Classifier()
classi.train(dadi,args.NN)
vally=Validator()
#Feature Search
feet = FeatureSearch(vally,dadi,classi)
match args.search:
case "forward":
feet.forwardSelection()
case "backward":
feet.backwardElimination()
case "simulated-annealing":
feet.simulatedAnnealing()