来自 电脑系统 2019-12-23 07:03 的文章
当前位置: 金沙澳门官网网址 > 电脑系统 > 正文

自定义类的疑惑,一个错误率

classabc(object):m=[]def__init__(self):foriinrange(5):self.m.append(i)k=[]foriinrange(3):k.add(abc())foriink:print(i.m)为什么运行结果是这样的?[0,1,2,3,4,0,1,2,3,4,0,1,2,3,4][0,1,2,3,4,0,1,2,3,4,0,1,2,3,4][0,1,2,3,4,0,1,2,3,4,0,1,2,3,4]

先上%1的```fromnumpyimport*importoperatorfromosimportlistdir#---------------------------------------------#分类模块#@params#inX:输入向量、手写体识别的测试向量#dataSet:训练集样本、手写体识别的训练集向量#labels:训练集对应的标签向量#k:最近邻居数目、本实验为3#---------------------------------------------defclassify0(inX,dataSet,labels,k):dataSetSize=dataSet.shape[0]#手写体样本集容量#(以下三行)距离计算diffMat=tile(inX,(dataSetSize,1))-dataSetsqDiffMat=diffMat**2sqDistances=sqDiffMat.sum(axis=1)distances=sqDistances**0.5#欧氏距离开平方sortedDistIndicies=distances.argsort()#距离排序的索引排序classCount={}#(以下两行)选择距离最小的k个点foriinrange(k):voteIlabel=labels[sortedDistIndicies[i]]classCount[voteIlabel]=classCount.get(voteIlabel,0)+1sortedClassCount=sorted(classCount.items(),#排序key=operator.itemgetter(1),reverse=True)returnsortedClassCount[0][0]#手写识别的测试代码defhandwritingClassTest():hwLabels=[]trainingFileList=listdir(path='trainingDigits')#获取目录内容m=len(trainingFileList)trainingMat=zeros((m,1024))foriinrange(m):#一下三行,从文件名解析分类数字fileNameStr=trainingFileList[i]fileStr=fileNameStr.split('.')[0]classNumStr=int(fileStr.split('_')[0])hwLabels.append(classNumStr)trainingMat[i,:]=img2vector('trainingDigits/%s'%fileNameStr)testFileList=listdir(path='testDigits')errorCount=0.0#错误个数计数器mTest=len(testFileList)#从测试数据中提取数据foriinrange(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("theclassifiercamebackwith:%d,therealansweris:%d"%(classifierResult,classNumStr))if(classifierResult!=classNumStr):errorCount+=1.0print("nthetotalnumberoferrorsis:%d"%errorCount)print("nthetotalerrorrateis:%f"%(errorCount/float(mTest)))#识别手写字体模块-图像转向量32x32to1x1024defimg2vector(filename):returnVect=zeros((1,1024))fr=open(filename)foriinrange(32):lineStr=fr.readline()forjinrange(32):returnVect[0,32*i+j]=int(lineStr[j])returnreturnVect```运行结果:thetotalnumberoferrorsis:10thetotalerrorrateis:0.010571然后上80%的```'''CreatedonSep16,2010kNN:kNearestNeighborsInput:inX:vectortocomparetoexistingdataset(1xN)dataSet:sizemdatasetofknownvectors(NxM)labels:datasetlabels(1xMvector)k:numberofneighborstouseforcomparison(shouldbeanoddnumber)有多少属性Output:themostpopularclasslabel@author:pbharrin'''fromnumpyimport*importoperatorfromosimportlistdirdefcreatDataSet():group=array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])labels=['A','A','B','B']returngroup,labelsdefclassify0(inX,dataSet,labels,k):#(坐标,测试向量组,标签/属性,迭代次数)dataSetSize=dataSet.shape[0]#dataSetSize返回值为4因为group有四行diffMat=tile(inX,(dataSetSize,1))-dataSet#x2-x1y2-y1sqDiffMat=diffMat**2#x**2y**2sqDistances=sqDiffMat.sum(axis=1)#x**2+y**2distances=sqDistances**0.5#根号下x**2+y**2sortedDistIndicies=distances.argsort()#距离排大小classCount={}foriinrange(k):voteIlabel=labels[sortedDistIndicies[i]]classCount[voteIlabel]=classCount.get(voteIlabel,0)+1sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)returnsortedClassCount[0][0]deffile2matrix(filename):fr=open(filename)numberOfLines=len(fr.readlines())#getthenumberoflinesinthefilereturnMat=zeros((numberOfLines,3))#preparematrixtoreturnclassLabelVector=[]#preparelabelsreturnfr=open(filename)index=0forlineinfr.readlines():line=line.strip()listFromLine=line.split('t')returnMat[index,:]=listFromLine[0:3]classLabelVector.append(int(listFromLine[-1]))index+=1returnreturnMat,classLabelVectordefautoNorm(dataSet):minVals=dataSet.min(0)maxVals=dataSet.max(0)ranges=maxVals-minValsnormDataSet=zeros(shape(dataSet))m=dataSet.shape[0]normDataSet=dataSet-tile(minVals,(m,1))normDataSet=normDataSet/tile(ranges,(m,1))#元素分割returnnormDataSet,ranges,minValsdefdatingClassTest():hoRadio=0.10datingDataMat,datingLabels=file2matrix('datingTestSet2.txt')normMat,ranges,minVals=autoNorm(datingDataMat)m=normMat.shape[0]numTestVecs=int(m*hoRadio)errorCount=0.0foriinrange(numTestVecs):classifierResult=classify0(normMat[i,:],normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],3)print("n测试结果为%d,正确的结果应该是%d"%(classifierResult,datingLabels[i]))if(classifierResult!=datingLabels[i]):errorCount+=1.0print("n该分类器错误率为%f"%(errorCount/float(numTestVecs)))#defdatingClassTest():#hoRatio=0.10#holdout10%#datingDataMat,datingLabels=file2matrix('datingTestSet2.txt')#loaddatasetfromfile#normMat,ranges,minVals=autoNorm(datingDataMat)#m=normMat.shape[0]#numTestVecs=int(m*hoRatio)#errorCount=0.0#foriinrange(numTestVecs):#classifierResult=classify0(normMat[i,:],normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],3)#print"theclassifiercamebackwith:%d,therealansweris:%d"%(classifierResult,datingLabels[i])#if(classifierResult!=datingLabels[i]):errorCount+=1.0#print"thetotalerrorrateis:%f"%(errorCount/float(numTestVecs))#printerrorCountdefclassifyPerson():resultList=['不喜欢','有点喜欢','特别喜欢']percentTats=float(input("每周玩多久游戏?:"))ffMiles=float(input("每年飞行多少英里?:"))iceCream=float(input("每周吃多少冰淇淋?:"))datingDataMat,datingLabels=file2matrix('datingTestSet2.txt')normMat,ranges,minVals=autoNorm(datingDataMat)inArr=array([ffMiles,percentTats,iceCream])result=classify0((inArr-minVals)/ranges,normMat,datingLabels,3)print("你将",resultList[result-1],"这个人")defimg2vector(filename):returnVect=zeros((1,1024))fr=open(filename)foriinrange(32):lineStr=fr.readline()forjinrange(32):returnVect[0,32*i+j]=int(lineStr[j])returnreturnVectdefhandwritingClassTest():hwLabels=[]trainingFileList=listdir(path='trainingDigits')m=len(trainingFileList)trainingMat=zeros((m,1024))foriinrange(m):fileNameStr=trainingFileList[i]fileStr=fileNameStr.split('.')[0]classNumStr=int(fileStr.split('_')[0])hwLabels.append(classNumStr)trainingMat[i,:]=img2vector('trainingDigits/%s'%fileNameStr)testFileList=listdir(path='testDigits')errorCount=0.0mTest=len(testFileList)foriinrange(mTest):fileNameStr=testFileList[i]fileStr=fileNameStr.split('.')[0]classNumStr=int(fileStr.split('_')[0])vectorUnderTest=img2vector('testDigits/%s'%fileNameStr)classifyResult=classify0(vectorUnderTest,trainingMat,hwLabels,3)print("测试结果为:%d,正确的结果为:%d"%(classifyResult,classNumStr))if(classifyResult!=classNumStr):errorCount+=1.0print("n错误结果总数为:%d"%errorCount)print("n错误率为:%f"%(errorCount/float(mTest)))```运行结果:错误结果总数为:777错误率为:0.821353

本文由金沙澳门官网网址发布于电脑系统,转载请注明出处:自定义类的疑惑,一个错误率

关键词: