forked from YomnaJehad/Drug-Pills-Identification-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_classifier.py
66 lines (56 loc) · 2.3 KB
/
knn_classifier.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
import csv
import random
import math
import operator
# calculation of euclidead distance
def calculateEuclideanDistance(variable1, variable2, length):
distance = 0
for x in range(length):
distance += pow(variable1[x] - variable2[x], 2)
return math.sqrt(distance)
# get k nearest neigbors
def kNearestNeighbors(training_feature_vector, testInstance, k):
distances = []
length = len(testInstance) - 1
for x in range(len(training_feature_vector)):
dist = calculateEuclideanDistance(testInstance,
training_feature_vector[x], length)
distances.append((training_feature_vector[x], dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
# votes of neighbors
def responseOfNeighbors(neighbors):
all_possible_neighbors = {}
for x in range(len(neighbors)):
response = neighbors[x][-1]
if response in all_possible_neighbors:
all_possible_neighbors[response] += 1
else:
all_possible_neighbors[response] = 1
sortedVotes = sorted(all_possible_neighbors.items(),
key=operator.itemgetter(1), reverse=True)
return sortedVotes[0][0]
# Load image feature data to training feature vectors and test feature vector
def loadDataset(filename, training_feature_vector=[]):
with open(filename) as csvfile:
lines = csv.reader(csvfile)
dataset = list(lines)
for x in range(len(dataset)):
for y in range(3):
dataset[x][y] = float(dataset[x][y])
training_feature_vector.append(dataset[x])
def main(training_data, test_data):
training_feature_vector = [] # training feature vector
test_feature_vector = []
test_feature_vector.append(test_data) # test feature vector
loadDataset(training_data, training_feature_vector)
classifier_prediction = [] # predictions
k = 3 # K value of k nearest neighbor
for x in range(len(test_feature_vector)):
neighbors = kNearestNeighbors(training_feature_vector, test_feature_vector[x], k)
result = responseOfNeighbors(neighbors)
classifier_prediction.append(result)
return classifier_prediction[0]