-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkNN.py
More file actions
69 lines (44 loc) · 1.7 KB
/
kNN.py
File metadata and controls
69 lines (44 loc) · 1.7 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 13 11:33:57 2016
@author: jay10
"""
import numpy as np
import operator
def createDataSet():
group = np.array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0.0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group, labels
def classify0(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0] # size of each data element in dataset
# convert input element in the shape of dataset to find difference between input element and each data element
diffMat = np.tile(inX, (dataSetSize, 1)) - dataSet
# Squared distance pairs
sqDiffMat = diffMat**2
# Sum of squared distance
sqdistances = sqDiffMat.sum(axis=1)
# actual distance
distances = sqdistances ** 0.5
# get sorting indices of the distances
sortedDistance = distances.argsort()
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistance[i]]
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
# sort the nearest classes in reverse
sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0] # This is the class our input element belongs to.
def file2matrix(filename):
fr = open(filename)
numberOflines = len(fr.readlines())
returnMat = np.zeros((numberOflines, 3))
classLabelVector = []
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
listFromline = line.split('\t')
returnMat[index, :] = listFromline[:3]
classLabelVector.append(listFromline[-1])
index += 1
return returnMat, classLabelVector