-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNN.py
67 lines (46 loc) · 1.78 KB
/
KNN.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
from math import sqrt
import pandas as pd
import numpy as np
from collections import Counter
def trainTestSplit(data):
rowNum = data.shape[0]
splitIndex = int(80 / 100 * rowNum)
train = data.iloc[:splitIndex].reset_index(drop=True) #reset indexes to start from index 0
test = data.iloc[splitIndex:].reset_index(drop=True)
return train, test
def eucledianDistance(p1, p2):
distance = np.sqrt(np.sum((p1 - p2) ** 2))
return distance
df = pd.read_csv('wine.csv').sample(frac=1)
classNum = len(set(df.iloc[:, 0]))
trainData, testData = trainTestSplit(df)
featuresTrain = trainData.iloc[:, 1:].values
labelsTrain = trainData.iloc[:, 0].values
trainNum = len(featuresTrain)
kk = sqrt(trainNum)
featuresTest = testData.iloc[:, 1:].values
labelsTest = testData.iloc[:, 0].values
# print(labels)
def KNNPredict(featuresTrain, labelsTrain, featureTest, k=15):
distances = []
#for every example in the training set, calculate eucledien distance against the test example
for i, point in enumerate(featuresTrain):
distances.append((i, eucledianDistance(featureTest, point)))
distances.sort(key = lambda x : x[1])
labels = []
for i, distance in distances[:k]:
labels.append(labelsTrain[i])
count = Counter(labels)
label = count.most_common()[0][0]
return label
def confusionMatrix(predictions, labels, classNum):
mat = np.zeros((classNum, classNum), dtype=np.int32)
for i in range(len(predictions)):
mat[predictions[i] - 1, labels[i] - 1] += 1
return mat
predictions = []
for point in featuresTest:
predictions.append(KNNPredict(featuresTrain, labelsTrain, point))
accuracy = np.sum(predictions == labelsTest) / len(labelsTest) * 100
print(accuracy)
print(confusionMatrix(predictions, labelsTest, classNum))