码迷,mamicode.com
首页 > 编程语言 > 详细

Python实现knn

时间:2016-08-14 19:15:47      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:


#
coding:utf-8 import numpy as np import operator import os def classify0(inX, dataSet, labels, k): dataSetSize = dataSet.shape[0] diffMat = np.tile(inX,(dataSetSize,1)) - dataSet sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis = 1) distances = sqDistances**0.5 sortedDistanceIndices = distances.argsort() classCount = {} for i in range(k): voteIlabel = labels[sortedDistanceIndices[i]] classCount[voteIlabel] = classCount.get(voteIlabel,0) +1 sortedClassCount = sorted(classCount.items(),key = operator.itemgetter(1), reverse = True) return sortedClassCount[0][0] def file2matric(filename): fr = open(filename) arrayOLine = fr.readlines() numberOfLines = len(arrayOLine) returnMat = np.zeros((numberOfLines,3)) classLabelVector = [] index = 0 for line in arrayOLine: line = line.strip() listFromLine = line.split(\t) returnMat[index,:] = listFromLine[0:3] if listFromLine[-1] == largeDoses: temp = 3 if listFromLine[-1] == smallDoses: temp = 2 if listFromLine[-1] == didntLike: temp = 1 classLabelVector.append(temp) index += 1 return returnMat, classLabelVector def autoNormal(dataset): minVals = dataset.min(0) maxVals = dataset.max(0) ranges = maxVals - minVals normDataset = np.zeros(np.shape(dataset)) m = dataset.shape[0] normDataset = dataset - np.tile(minVals,(m,1)) normDataset = normDataset/np.tile(ranges,(m,1)) return normDataset, ranges, minVals def classifyPersion(): resultList = [not at all, in small doses, in large doses] percentTats = float(raw_input("percentage of time spent play video games?")) ffMiles = float(raw_input("frequent filer niles earned per year?")) iceCream = float(raw_input("liters of ice cream consumed per year?")) datingDataMat, datingLabels = file2matric(r".\datingTestSet.txt") normalMat , ranges, minVals = autoNormal(datingDataMat) inArr = np.array([ffMiles,percentTats,iceCream]) classifierResult = classify0((inArr - minVals)/ranges, normalMat, datingLabels,3) print "You will probably like this persion :" , resultList[classifierResult - 1] def img2vertor(filename): returnVect = np.zeros((1,1024)) fr = open(filename,r+) 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 = [] traingFileList = os.listdir(r".\digits\trainingDigits") m = len(traingFileList) trainingMat = np.zeros((m,1024)) for i in range(m): fileNameStr = traingFileList[i] fileStr = fileNameStr.split(.)[0] classNumStr = int(fileStr.split(_)[0]) hwLabels.append(classNumStr) trainingMat[i,:] = img2vertor(r".\digits\trainingDigits\%s" % fileNameStr) testFileList = os.listdir(r".\digits\testDigits") mtest = len(testFileList) errorCount = 0.0 for i in range(mtest): fileNameStr = testFileList[i] fileStr = fileNameStr.split(".")[0] classNumStr = fileStr.split("_")[0] vectorUnderTest = img2vertor(r".\digits\testDigits\%s" % fileNameStr) classifierResult = classify0(vectorUnderTest, trainingMat, hwLabels, 3) print "the classifier came back with: %d ,the real answer is :%s " % (classifierResult,classNumStr) if(classifierResult != int(classNumStr)): errorCount += 1.0 print "\n the total number of error is %d" % errorCount print "\n the total error rate is %f" % (errorCount/float(mtest))

以上代码包含两个小项目:

第一个是使用knn算法改进约会网站的配对效果,使用Python交互界面运行

技术分享

先后输入参数10   10000    0.5      结果为in small doses

数据点此出下载  提取码:ue4a

 

第二个小项目是手写数字识别系统

同样是使用交互界面运行

import knn

knn.handwritingclasstest()

数据点此处下载  提取码:9qd1

 

本代码是依据《机器学习实战》这本书编写。

Python实现knn

标签:

原文地址:http://www.cnblogs.com/zangyu/p/5770595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!