-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.py
95 lines (81 loc) · 2.77 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################################
# Name:
# knn
# Description:
# knn
# Author:
# wesley wu
# Python:
# 3.5
# Version:
# 1.0
########################################################################
import csv as csv
import random as random
import math as math
import operator as operator
# load the data and split it into two data set
def prepare_dataset(filename ):
# read the data from csv into the list
with open(filename, 'r') as f:
reader = csv.reader(f)
orig_data = list(reader)
# Divide the data into two parts, one for training and the other for verification
random.shuffle(orig_data)
train_data = orig_data[:int(0.7 * 30)]
val_data = orig_data[int(0.7 * 30):]
return train_data, val_data
# calculate the distance
def calculate_euclidean_distance(s1,s2):
ed = 0.0
# exclude the label
for i in range(len(s1) - 1):
ed += pow((float(s1[i]) - float(s2[i])), 2) # euclidean distance
ed = math.sqrt(ed)
return ed
# find the nearest neighbors, the number of neighbors is defined by k value
def find_neighbors(dataset, sample, k):
distances = []
for x in range(len(dataset)):
dist = calculate_euclidean_distance(dataset[x],sample )
distances.append((dataset[x], dist))
distances.sort(key = operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
# do the prediction, the sample point will evaluate the vote of the k nearest neighbors
def knn_prediction(neighbors):
classVotes = {}
for x in range(len(neighbors)):
response = neighbors[x][-1]
if response in classVotes:
classVotes[response] += 1
else:
classVotes[response] = 1
sortedVotes = sorted(classVotes.items(), key=operator.itemgetter(1), reverse=True)
# the class with largest voted numbers
return sortedVotes[0][0]
def getAccuracy(valset, predictions):
correct = 0
for x in range(len(valset)):
if valset[x][-1] == predictions[x]:
correct += 1
return (correct / float(len(valset))) * 100.0
# main loop begin here
train_data, val_data = prepare_dataset("knn.csv")
# generate predictions
predictions=[]
# the number of nearest neighbors
k = 3
for x in range(len(val_data)):
neighbors = find_neighbors(train_data, val_data[x], k)
#print("x:{} and it's neighbors:{}".format(val_data[x],neighbors))
result = knn_prediction(neighbors)
predictions.append(result)
print('> predicted=' + repr(result) + ', actual=' + repr(val_data[x][-1]))
accuracy = getAccuracy(val_data, predictions)
print('Accuracy: ' + repr(accuracy) + '%')
print(calculate_euclidean_distance(train_data[0],train_data[1]))