Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayadams2552 committed Dec 2, 2024
2 parents 48ceecb + 21d6c66 commit fc98581
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions theProject.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def loadTestData(self,testSet=SMALL_DATA): # SMALL_DATA or BIG_DATA
data = np.loadtxt(testSet)
self.labels = data[:,0].astype(int)
self.features = data[:,1:]
logging.info(f"Successfully Loaded into Matrix of Size {data.shape}")
logging.info(f"Data Successfully Loaded into Matrix of Size {data.shape}")

def loadFeatureList(self,featureList):
featureList=np.array(featureList) - 1 #Label col removed -> shift left 1
self.featureList=featureList
logging.info(f"Successfully Loaded Features Set!")
logging.info(f"Data Successfully Loaded {featureList}!")

class Classifier: # Calculates distance between every point for NN
data = Data()
Expand All @@ -59,21 +59,22 @@ def train(self,data, kNN = 1):
self.data = data
self.kNN = kNN

def test(self,testIndex) -> int:
def test(self,testIndex : int,output = True) -> int:
distList = [] #Heap (Dist to testIndex, Index)
logging.info(f"Calculating the Distance between index {testIndex} and the other datapoints...")
for R in range(len(self.data.features)):
if R == testIndex: continue
heapq.heappush(distList,(self.__calcDistance__(R,testIndex),R))
counter = Counter()
for _ in range(self.kNN): # Get k shorests distances to testIndex
_ ,index = heapq.heappop(distList)
counter[self.data.labels[index]] += 1
return counter.most_common(1)[0][0]
guess = counter.most_common(1)[0][0]
if output: logging.info(f"Index {testIndex} Classifier Test | Guess/Actual: {guess}/{self.data.labels[testIndex]}")
return guess


#Returns euclidian distance between testindex and row R
def __calcDistance__(self, R, testIndex) -> float:
def __calcDistance__(self, R : int, testIndex : int) -> float:
currentSum : float = 0
for C in self.data.featureList:
currentSum += (self.data.features[testIndex,C]-self.data.features[R,C])**2
Expand All @@ -83,7 +84,7 @@ class Validator: #Computes classifier's accuracy
def __init__(self):
pass

def evaluate(self, data:Data, classifier:Classifier, featureList = None):
def evaluate(self, data : Data, classifier : Classifier, featureList = None):
correct = 0
accuracy = 0
if featureList:
Expand All @@ -102,7 +103,7 @@ def evaluate(self, data:Data, classifier:Classifier, featureList = None):
class FeatureSearch:
featureList = []

def __init__(self,featureCount):
def __init__(self,featureCount : int):
self.featureList = list(range(featureCount))

def evaluate(self):
Expand Down

0 comments on commit fc98581

Please sign in to comment.