标签:code datasets ack getter 有序字典 eve blog lis auto
程序主体:
1 # -*- coding:utf-8 -*- 2 from numpy import * 3 import operator 4 import math 5 import matplotlib 6 import matplotlib.pyplot as plt 7 import numpy as np 8 import random 9 import collections 10 11 def classify0(inX,dataSet,labels,k): 12 dataSetSize = dataSet.shape[0] 13 diffMat = tile(inX,(dataSetSize,1)) - dataSet 14 sqDiffMat = diffMat**2 15 print sqDiffMat 16 sqDistances = sqDiffMat.sum(axis = 1) 17 distances = sqDistances.argsort() 18 # sortedDistIndicies = distances.argsort() 19 classCount=collections.OrderedDict() 20 for i in range(k): 21 voteIlabel = labels[distances[i]] 22 classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1 23 print classCount 24 sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter(1),reverse = True) 25 print sortedClassCount 26 return sortedClassCount[0][0] 27 28 def file2matrix(filename): 29 fr = open(filename) 30 arrayOLines = fr.readlines() 31 numberOfLines = len(arrayOLines) 32 returnMat = zeros((numberOfLines,3)) 33 classLabelVector = [] 34 index = 0 35 for line in arrayOLines: 36 line = line.strip() 37 listFromLine = line.split(‘ ‘) 38 returnMat[index,:] = listFromLine[0:3] 39 classLabelVector.append(int(listFromLine[0])) 40 index+=1 41 return returnMat,classLabelVector 42 43 def autoNorm(dataSet): 44 minVals = dataSet.min(0) 45 maxVals = dataSet.max(0) 46 ranges = maxVals - minVals 47 normDataSet = zeros(shape(dataSet)) 48 m = dataSet.shape[0] 49 normDataSet = dataSet - tile(minVals,(m,1)) 50 normDataSet = normDataSet/tile(ranges,(m,1)) 51 return normDataSet,ranges,minVals 52 53 datingdataMat,datingLabels = file2matrix(‘/Users/tiemuer/PycharmProjects/kNN/data.txt‘) 54 fig = plt.figure() 55 ax = fig.add_subplot(111) 56 ax.scatter(datingdataMat[:,0],datingdataMat[:,1],15.0*array(datingLabels),15.0*array(datingLabels)) 57 plt.show() 58 59 def datingClassTest(): 60 hoRatio = 0.10 61 datingdataMat,datingLabels = file2matrix(‘/Users/tiemuer/PycharmProjects/kNN/data.txt‘) 62 normMat,ranges,minVals = autoNorm(datingdataMat) 63 m = normMat.shape[0] 64 numTestVecs = int(m*hoRatio) 65 errorCount = 0.0 66 n = [] 67 for i in range(numTestVecs): 68 n.append(int(random.randint(0,m))) 69 for i in n: 70 classifierResult = classify0(normMat[i,:],normMat[:m,:],datingLabels[:m],3) 71 print "the classifier cameback with %d,the real answer is:%d"%(classifierResult,datingLabels[i]) 72 if(classifierResult!=datingLabels[i]):errorCount+=1.0 73 print "the total error rate is %f"%(errorCount/float(numTestVecs)) 74 75 76 datingClassTest()
算法改进
初学python,记录学习笔记
strip() 方法用于移除字符串头尾指定的字符(默认为空格)。例如:str.strip([chars])
split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串。例如:split(‘ ‘,1)仅对于空格分割一次。
标签:code datasets ack getter 有序字典 eve blog lis auto
原文地址:http://www.cnblogs.com/tiemuer/p/7822633.html