#encoding:utf-8
from numpy import *
import operator
import matplotlib
import matplotlib.pyplot as plt from os import listdir def makePhoto(returnMat,classLabelVector): #创建散点图
fig = plt.figure()
ax = fig.add_subplot(111) #例如参数为349时,参数349的意思是:将画布分割成3行4列,图像画在从左到右从上到下的第9块
ax.scatter(returnMat[:,1],returnMat[:,2],15.0*array(classLabelVector),15.0*array(classLabelVector)) #前两个函数参数的意义是横坐标和纵坐标
plt.show() def classify0(inX, dataSet, labels, k): #k近邻算法 inX为分类向量 dataSet为训练数据集 labels为标签列表 k:选择距离最小的k个点
dataSetSize = dataSet.shape[0] #获得dataSet的第一维长度
diffMat = tile(inX, (dataSetSize, 1)) - dataSet #将向量inX重复1次,共dataSetSize行。然后减去dataSet
sqDiffMat = diffMat ** 2 #diffMat矩阵的每个元素平方
sqDistances = sqDiffMat.sum(axis=1) #按行求和,即求每一行的和,得到一个list
distances = sqDistances ** 0.5 #对sqDistances的每个元素开方
sortedDistIndicies = distances.argsort() #返回distances从小到大的索引
#例如,x=np.array([3.2.1]) np.argsort(x) 得到的结果为array([1.2.0])
classCount = {} #python花括号代表代表dict字典数据类型,[]代表list,()代表元组,(55,)一个值的元组
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]] #依次取出最小的元素
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 #如果存在加1,不存在默认为0
sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) #第一个参数为比较的list,第二个参数为比较的对象(这里指的是键值对的value),第三个参数是升序或降序(默认为false(升序排列))
return sortedClassCount[0][0] #取得次数最多的key 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 file2matrix(filename): #解析文本
fr = open(filename)
numberOfLines = len(fr.readlines()) #得到文本的行数
returnMat = zeros((numberOfLines, 3)) #构造一个行数为文本行数,列数为3的0矩阵
classLabelVector = [] #标签列表
fr = open(filename)
index = 0
for line in fr.readlines(): #读取文件的每一行
line = line.strip() #删除符号,此处参数为空,表示删除空白符
listFromLine = line.split('\t') #以\t符号为分隔符
returnMat[index, :] = listFromLine[0:3] #将listFromLine的0到3赋值给returnMat的index行
classLabelVector.append(int(listFromLine[-1])) #将listFromLine的最后一个元素添加到classLabelVector
index += 1 #下标加1
return returnMat, classLabelVector def autoNorm(dataSet): #归一化特征值
minVals = dataSet.min(0) #取每列的最小值赋值给minVals,是一个list
maxVals = dataSet.max(0) #取每列的最大值赋值给maxVals
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.50 # 测试用例占用比例
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) #第numTestVecs到m是样本数据,前m个是测试数据
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))
print errorCount #输入某人的信息,便得出对对方喜欢程度的预测值
def classifyPerson():
resultList = ['一点也不喜欢', '有点喜欢', '非常喜欢']
percentTats = float(raw_input("玩视频游戏所耗时间百分比是:"))#输入
ffMiles = float(raw_input("飞行里程数是:"))
iceCream = float(raw_input("每周消费的冰淇淋公升数是:"))
datingDataMat, datingLabels = file2matrix('datingTestSet2.txt') #读入样本文件,其实不算是样本,是一个标准文件
normMat, ranges, minVals = autoNorm(datingDataMat)#归一化
inArr = array([ffMiles, percentTats, iceCream])#组成测试向量
classifierResult = classify0((inArr-minVals)/ranges, normMat, datingLabels,3)#进行分类
print '你对这种人的看法可能是:', resultList[classifierResult - 1]#打印结果 def img2vector(filename): #将图片转化成向量
returnVect = zeros((1, 1024))
fr = open(filename)
for i in range(32):
lineStr = fr.readline()
for j in range(32):
returnVect[0, 32 * i + j] = int(lineStr[j])
return returnVect def handwritingClassTest():
hwLabels = []
trainingFileList = listdir('trainingDigits') # 加载文件夹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 "分类器返回的结果: %d, 真实结果: %d" % (classifierResult, classNumStr)
if (classifierResult != classNumStr): errorCount += 1.0 #计算得到结果与标签比较
print "\n错误总数为: %d" % errorCount
print "\n错误率为: %f" % (errorCount / float(mTest)) if __name__ == '__main__':
# print("main")
# returnMat, classLabelVector = file2matrix('datingTestSet2.txt')
# #makePhoto(returnMat,classLabelVector)
# normDataSet, ranges, minVals = autoNorm(returnMat)
# print(normDataSet)
# print(ranges)
# print(minVals)
#classifyPerson()
# textVector = img2vector("testDigits/0_13.txt")
# print textVector[0,0:31]
handwritingClassTest()