## 机器学习实战------k-近邻算法

```from numpy import *
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```

```import sys
sys.path.append("D:\xx\xxxxxx")

import KNN```

`group,labels = KNN.createDataSet()`

k-近邻算法的伪代码为：

```def classify0(inx,dataSet,labels,k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inx,(dataSetSize,1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis = 1)
distances = sqDistances **0.5
sortedDistIndicies = distances.argsort()
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
sortedClassCount = sorted(classCount.items(),
key = operator.itemgetter(1),reverse = True)
return sortedClassCount[0][0]
```

`sortedClassCount = sorted(classCount.items()`

`sortedClassCount = sorted(classCount.iteritems()`

```import KNN
group,labels = KNN.createDataSet()
KNN.classify0([0,0],group,labels,3)```

```import KNN
group,labels = KNN.createDataSet()
KNN.classify0([0.9,1],group,labels,3)```

2018.1.15更新

```def classify0(inx,dataSet,labels,k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inx,(dataSetSize,1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis = 1)
distances = sqDistances **0.5
sortedDistIndicies = distances.argsort()
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
sortedClassCount = sorted(classCount.items(),
key = operator.itemgetter(1),reverse = True)
return sortedClassCount[0][0]```

1.numpy.tile(a,b)函数，是在列方向上重复N次，在这是重复dataSetSize次

```>>> import numpy
>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次，默认行1次
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> numpy.tile([0,0],(1,1))#在列方向上重复[0,0]1次，行1次
array([[0, 0]])
>>> numpy.tile([0,0],(2,1))#在列方向上重复[0,0]1次，行2次
array([[0, 0],
[0, 0]])
>>> numpy.tile([0,0],(3,1))
array([[0, 0],
[0, 0],
[0, 0]])
>>> numpy.tile([0,0],(1,3))#在列方向上重复[0,0]3次，行1次
array([[0, 0, 0, 0, 0, 0]])
>>> numpy.tile([0,0],(2,3))<span style="font-family: Arial, Helvetica, sans-serif;">#在列方向上重复[0,0]3次，行2次</span>
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]) ```
2. argsort函数返回的是数组值从小到大的索引值

4.1.1准备数据

```def file2matrix(filename):
fr = open(filename)
returnMat = zeros((numberOfLines,3))
classLabeVector = []
fr = open(filename)
index = 0
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]
classLabeVector.append(int(listFromLine[3]))
index += 1
return returnMat,classLabeVector```

```import sys
sys.path.append("D:\xx\xxxx")
import KNN```

```import os
os.chdir('D:\\xx\\machinelearning\\MLiA_SourceCode')
datingDataMat,datingLabels = KNN.file2matrix('datingTestSet2.txt')```

4.1.2分析数据：使用Matplotlib创建散点图

```import matplotlib
import matplotlib.pyplot as plt
from numpy import array
fig = plt.figure()
ax.scatter(datingDataMat[:,0],datingDataMat[:,1],15.0 * array(datingLabels),15.0 * array(datingLabels))
plt.show()```

4.2.3准备数据：归一化数值

```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```

```from imp import reload
`normMat,ranges,minVals = KNN.autoNorm(datingDataMat)`

4.2.4测试算法，完整程序验证分类器

```def dataingClassTest():
hoRatio = 0.10
datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')
normMat,ranges,minVals = autoNorm(datingDataMat)
m = normMat.shape[0]
numTestVecs = int(m * hoRatio)
errorCount = 0.0
for i in range(numTestVecs):
classifierResult = classify0(normMat[i,:],normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],3)
print('the classifier came back with:%d,the real answer is : %d'%(classifierResult,datingLabels[i]))
if (classifierResult != datingLabels[i]): errorCount += 1.0
print('the total error rate is: %f'%(errorCount/float(numTestVecs)))```

```reload(KNN)
KNN.dataingClassTest()```

4.2.5 使用算法构建完整可用的系统

```def classifyPerson():
resultList = ['not at all','in small doses','in large doses']
percentTats = float(input('percentage of time spent playing video games?'))
ffMiles = float(input('frequent flier miles earned per year?'))
iceCream = float(input('liters of ice cream consumed per year?'))
datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')
normMat,ranges,minVals = autoNorm(datingDataMat)
inArr = array([ffMiles,percentTats,iceCream])
classifierResult = classify0((inArr - minVals)/ranges,normMat,datingLabels,3)
print('You will probably like this person:',resultList[classifierResult -1])
```

2018.1.16修改

```from os import listdir
def handwritingClassTest():
hwLabels = []
trainingFileList = listdir('trainingDigits')
m = len(trainingFileList)
trainingMat = zeros((m,1024))
for i in range(m):
fileNameStr = trainingFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
hwLabels.append(classNumStr)
trainingMat[i,:] = img2vector('trainingDigits/%s' % fileNameStr)
testFileList = listdir('testDigits')
errorCount = 0.0
mTest = len(testFileList)
for i in range(mTest):
fileNameStr = testFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
vectorUnderTest = img2vector('testDigits/%s' % fileNameStr)
classifierResult = classify0(vectorUnderTest,trainingMat,hwLabels,3)
print('the classifier came back with: %d, the real answer is: %d'\
%(classifierResult,classNumStr))
if (classifierResult != classNumStr):errorCount += 1.0
print('\nthe total number of errors is: %d '%errorCount)
print('\nthe total error rate is: %f '%(errorCount/float(mTest)))```

`KNN.handwritingClassTest()`