Skip to content

Commit

Permalink
refactor printing
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmshane committed Nov 18, 2024
1 parent 6f4485a commit 703f285
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions theProject.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import math
import random
from dataclasses import dataclass
from pathlib import Path


Expand Down Expand Up @@ -48,6 +47,7 @@ def forwardSelection(self)->list:
currentFeatures.add(self.featureList[i])
eval = self.evaluate()
print(f"{currentFeatures} Evaluated at {eval}")

if eval > bestChildAccuracy[0]:
bestChildAccuracy = (eval,i)

Expand All @@ -57,18 +57,19 @@ def forwardSelection(self)->list:
print(Printer.searchQuit)
break

currentFeatures.add(self.featureList[bestChildAccuracy[1]]) # Add back best branch
print(f"\nAdding Feature {self.featureList[bestChildAccuracy[1]]}" )
print(f"New Feature Set: {currentFeatures} ~ Accuracy {bestChildAccuracy[0]}\n")
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 currentFeatures

def backwardElimination(self)->list:
n = len(self.featureList)
parentAccuracy = -math.inf
currentFeatures=set(self.featureList)
currentFeatures = set(self.featureList)
depth = 0
print(Printer.searchStartBackward)
while depth < n:
Expand All @@ -79,26 +80,29 @@ def backwardElimination(self)->list:
currentFeatures.remove(self.featureList[i])
eval = self.evaluate()
print(f"Evaluated {currentFeatures} at {eval} ")

if eval > bestChildAccuracy[0]:
bestChildAccuracy=(eval,i)
bestChildAccuracy = (eval, i)

currentFeatures.add(self.featureList[i])

if bestChildAccuracy[0] < parentAccuracy: # No better options dont add -> exit
print(Printer.searchQuit)
break

currentFeatures.remove(self.featureList[bestChildAccuracy[1]]) #Removing the best child from feature list
print(f"\nRemoving Feature {self.featureList[bestChildAccuracy[1]]}" )
print(f"New Feature Set: {currentFeatures} ~ Accuracy {bestChildAccuracy[0]}\n")
featureChanged = self.featureList[bestChildAccuracy[1]]
currentFeatures.remove(featureChanged) #Removing the best child from feature list
Printer.printFeatureChange(featureChanged,currentFeatures,bestChildAccuracy[0],False)
parentAccuracy = bestChildAccuracy[0]
depth += 1


Printer.printFeatureListSelected(currentFeatures,parentAccuracy)
return currentFeatures



class Printer:
mainWelcome : str = "Welcome to SZIMM011 and LADAM020's Project 2!"
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..."
Expand All @@ -110,13 +114,26 @@ def featureCountPrompt() -> int:
if valIn.isnumeric() and int(valIn) < MAX_FEATURE_COUNT:
return int(valIn)
return DEFAULT_FEATURE_COUNT


@staticmethod
def printFeatureListSelected(Currentfeatures,accuracy):
print()
print(f"Best Feature Set Found: {Currentfeatures}")
print(f"Acurracy: {accuracy}\n")

@staticmethod
def printFeatureChange(featureChanged,currentFeatures,accuracy,add=True):
if add: print("\nAdd", end="")
else: print("\nRemove",end="")
print(f"Feature {featureChanged}" )
print(f"New Feature Set: {currentFeatures} ~ Accuracy {accuracy}")

#MAIN
if __name__ == "__main__":
print(Printer.mainWelcome)
feet = FeatureSearch()
#print(f"Forward Selection Set: {feet.forwardSelection()}\n")
#print(f"Backward Elimination Set: {feet.backwardElimination()}\n")
hey = Data()
hey.loadTestData()
feet.forwardSelection()
feet.backwardElimination()
#hey = Data()
#hey.loadTestData()

0 comments on commit 703f285

Please sign in to comment.