简单的说,k-近邻算法采用测量不同特征值之间的距离方法进行分类。
优点:精度高、对异常值不敏感、无数据输入假定。
缺点:计算复杂度高、空间复杂度高。
适用数据范围:数值型和标称型。
试用k-近邻算法改进约会网站的配对效果
from numpy import *
import operator
import os
def creatDataSet():
group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group, labels
# inX用于分类的输入向量 dataSet训练样本集 labels标签向量 k表示选择最近邻居的数目
def classify0(inX, dataSet, labels, k):
# 返回矩阵第一纬度的长度
dataSetSize = dataSet.shape[0]
# 输入向量与样本集的欧氏距离 tile()将矩阵平铺开
diffMat = tile(inX, (dataSetSize, 1)) - dataSet
sqDiffMat = diffMat ** 2
# 按行相加求和 axis = 0表示按列相加,axis = 1表示按行相加
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances ** 0.5
# argsort()排序后,返回下标向量
sortedDistIndicies = distances.argsort()
classCount = {}
for i in range(k):
voteILabel = labels[sortedDistIndicies[i]]
# dict.get(key,value) 若没有找到key对应的value则返回设置的value
classCount[voteILabel] = classCount.get(voteILabel, 0) + 1
sortedClassCount = sorted(classCount.items(),
key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0]
def file2matrix(filename):
fr = open(filename)
arrayOfLines = fr.readlines()
numberOfLines = len(arrayOfLines)
# numberOfLines行 3列的零矩阵
returnMat = zeros((numberOfLines, 3))
classLabelVector = []
index = 0
for line in arrayOfLines:
# 去除前后空格、回车
line = line.strip()
# 按照\t将每行分割开来
listFromLine = line.split('\t')
# 将前三行的内容填充到对应的行数矩阵 逗号表示维度
returnMat[index:index + 1, :] = listFromLine[0:3]
# 按照喜欢程度添加标签
classLabelVector.append(int(listFromLine[-1]))
index += 1
return returnMat, classLabelVector
# 归一化特征值
def autoNorm(dataSet):
# 一维数组,每一列的最小值,0表示列
minVals = dataSet.min(0)
# 一维数组,每一列的最大值,0表示列
maxVals = dataSet.max(0)
ranges = maxVals - minVals
normDataSet = zeros(shape(dataSet))
m = dataSet.shape[0]
# 减去最小值扩展成的矩阵,共有m行
normDataSet = dataSet - tile(minVals, (m, 1))
# 归一化公式 newValue = (oldValue - min) / (max - min)
# ranges = maxVals - minVals
normDataSet = normDataSet / tile(ranges, (m, 1))
return normDataSet, ranges, minVals
def datingClassTest():
hoRatio = 0.10
datingDataMat,datingLabels = file2matrix('datingTestSet.txt')
normat, ranges, minVals = autoNorm(datingDataMat)
m = normat.shape[0]
numTestVecs = int(m * hoRatio)
errorCount = 0.0
for i in range(numTestVecs):
classifierResult = classify0(normat[i, :], normat[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)))
# 将图像转换成向量
def img2vector(filename):
# 创建一个m = 1,n = 1024的矩阵
returnVect = zeros((1, 1024))
fr = open(filename)
for i in range(32):
lineStr = fr.readline()
for j in range(32):
returnVect[0, i * 32 + j] = int(lineStr[j])
return returnVect
# 手写识别测试
def handwritingClassTest():
hwLabels = []
trainingFileList = os.listdir('trainingDigits')
m = len(trainingFileList)
# 文本文件格式为32*32 为了使用分类器解决该问题,必须改成行向量
trainingMat = zeros((m, 1024))
for i in range(m):
fileNameStr = trainingFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('_')[0])
hwLabels.append(classNumStr)
# 将第i个图像导入到测试集合中
trainingMat[i, :] = img2vector('trainingDigits/%s' % fileNameStr)
testFileList = os.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("\n the total number of errors is: %d" % errorCount)
print("\n the total error rate is: %f" % (errorCount/float(mTest)))
if __name__ == '__main__':
datingDataMat, datingLabels = file2matrix("datingTestSet.txt")
'''
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
#add_subplot(mnp)添加子轴、图。subplot(m,n,p)或者subplot(mnp)此函数最常用:
subplot是将多个图画到一个平面上的工具。其中,m表示是图排成m行,n表示图排成n列,
也就是整个figure中有n个图是排成一行的,一共m行,如果第一个数字是2就是表示2行图。
p是指你现在要把曲线画到figure中哪个图上,最后一个如果是1表示是从左到右第一个位置。
ax = fig.add_subplot(111)
#以第二列和第三列为x,y轴画出散列点,给予不同的颜色和大小
ax.scatter(datingDataMat[:,1], datingDataMat[:,2],15.0*array(map(int,datingLabels)),15.0*array(map(int,datingLabels)))
#scatter(x,y,s=1,c="g",marker="s",linewidths=0)
#s:散列点的大小,c:散列点的颜色,marker:形状,linewidths:边框宽度
plt.show()
'''