forked from liulei2018/mlpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkNN.py
91 lines (75 loc) · 2.94 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
from os import listdir
from numpy import *
from matplotlib import *
import matplotlib.pyplot as plt
import operator
def createDataset():
group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group, labels
def classfy0(intX, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
diffMat = tile(intX, (dataSetSize, 1)) - dataSet
sqDiffMat = diffMat ** 2
sqlDistance = sqDiffMat.sum(axis=1)
distances = sqlDistance ** 0.5
sortedDisIndicies = distances.argsort()
classCount = {}
for i in range(k):
voteIlable = labels[sortedDisIndicies[i]]
classCount[voteIlable] = classCount.get(voteIlable, 0) + 1
sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)
sortedClassCount[0]
return sortedClassCount[0][0]
def file2matrix(filename):
fr = open(filename)
numberOfLines = len(fr.readlines())
returnMat = zeros((numberOfLines, 3))
classLabVector = []
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
listFromLine = line.split('\t')
returnMat[index, :] = listFromLine[0:3]
# classLabVector.append(int(listFromLine[-1]))
classLabVector.append(int(listFromLine[-1]))
index += 1
return returnMat, classLabVector
def autoNorm(dataSet):
minVals = dataSet.min(0)
maxVals = dataSet.max(0)
ranges = maxVals - minVals
normDataSet = zeros(shape(dataSet))
m = dataSet.shape[0]
normDataSet = dataSet - tile(minVals, (m, 1))
normDataSet = normDataSet / tile(ranges, (m, 1))
return normDataSet, ranges, minVals
def datingClassTest():
hoRatio = 0.10
datingDataMat, datingLabels = file2matrix("D:\opensource\machinelearninginaction\Ch02\datingTestSet2.txt")
print(datingDataMat)
print(datingLabels)
norMat, ranges, minVals = autoNorm(datingDataMat)
m = norMat.shape[0]
numTestVecs = int(m * hoRatio)
errorCount = 0
for i in range(numTestVecs):
classifierResult = classfy0(norMat[i, :], norMat[numTestVecs:m, :], datingLabels[numTestVecs:m], 3)
print("the classfier came back with :%d ,the real answer is : %d " % (classifierResult, datingLabels[i]))
if (classifierResult != datingLabels[i]): errorCount += 1.0
print("the total error is %f " % (errorCount / float(numTestVecs)))
listdir('ad')
if __name__ == "__main__":
groupbl, labels = createDataset()
# print(2323)
# print(classfy0([0, 0], groupbl, labels, 3))
# datingDataMat, datingLabels = file2matrix("D:\opensource\machinelearninginaction\Ch02\datingTestSet2.txt")
# print(datingDataMat)
# print(datingLabels[0:20])
# fig = plt.figure()
# ax = fig.add_subplot(111)
# ax.scatter(datingDataMat[:, 1], datingDataMat[:, 2],15.0 * array(datingLabels), 11.0 * array(datingLabels))
# # 15.0 * array(datingLabels), 15.0 * array(datingLabels)
# plt.show()
# datingClassTest()