<s> import operator <EOL> from numpy import * <EOL> def handwritingClassTest ( ) : <EOL> hwLabels = [ ] <EOL> trainingFileList = listDirInMac ( '<STR_LIT>' ) <EOL> m = len ( trainingFileList ) <EOL> trainingMat = zeros ( ( m , <NUM_LIT> ) ) <EOL> for i in range ( m ) : <EOL> fnameStr = trainingFileList [ i ] <EOL> classStr = fnameStr . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] <EOL> hwLabels . append ( classStr ) <EOL> trainingMat [ i , : ] = img2vector ( '<STR_LIT>' % fnameStr ) <EOL> testFileList = listDirInMac ( '<STR_LIT>' ) <EOL> errorCount = <NUM_LIT:0.0> <EOL> mTest = len ( testFileList ) <EOL> for i in range ( mTest ) : <EOL> fnameStr = testFileList [ i ] <EOL> classStr = fnameStr . split ( '<STR_LIT:_>' ) [ <NUM_LIT:0> ] <EOL> vTest = img2vector ( '<STR_LIT>' % fnameStr ) <EOL> classifierResult = classify0 ( vTest , trainingMat , hwLabels , <NUM_LIT:3> ) <EOL> print "<STR_LIT>" % ( classifierResult , classStr ) , <EOL> if ( classifierResult != classStr ) : <EOL> errorCount += <NUM_LIT:1.0> <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % errorCount <EOL> print "<STR_LIT>" % ( errorCount / float ( mTest ) ) <EOL> def classify0 ( inX , dataSet , labels , k ) : <EOL> dataSetSize = dataSet . shape [ <NUM_LIT:0> ] <EOL> diffMat = tile ( inX , ( dataSetSize , <NUM_LIT:1> ) ) - dataSet <EOL> sqDiffMat = diffMat ** <NUM_LIT:2> <EOL> sqDistances = sqDiffMat . sum ( axis = <NUM_LIT:1> ) <EOL> distances = sqDistances ** <NUM_LIT:0.5> <EOL> sortedDistIndicies = distances . argsort ( ) <EOL> classCount = { } <EOL> for i in range ( k ) : <EOL> voteIlabel = labels [ sortedDistIndicies [ i ] ] <EOL> classCount [ voteIlabel ] = classCount . get ( voteIlabel , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> sortedClassCount = sorted ( classCount . iteritems ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) , reverse = True ) <EOL> return sortedClassCount [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def img2vector ( fname ) : <EOL> f = open ( fname ) <EOL> returnVec = zeros ( ( <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> lineStr = f . readline ( ) <EOL> if lineStr [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> lineStr = lineStr [ : - <NUM_LIT:1> ] <EOL> rownum = len ( lineStr ) <EOL> for j in range ( rownum ) : <EOL> returnVec [ <NUM_LIT:0> , rownum * i + j ] = int ( lineStr [ j ] ) <EOL> return returnVec <EOL> def file2matrix ( filename ) : <EOL> fr = open ( filename ) <EOL> numberOfLines = len ( fr . readlines ( ) ) <EOL> returnMat = zeros ( ( numberOfLines , <NUM_LIT:3> ) ) <EOL> classLabelVector = [ ] <EOL> fr = open ( filename ) <EOL> index = <NUM_LIT:0> <EOL> for line in fr . readlines ( ) : <EOL> line = line . strip ( ) <EOL> listFromLine = line . split ( '<STR_LIT:\t>' ) <EOL> returnMat [ index , : ] = listFromLine [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> classLabelVector . append ( listFromLine [ - <NUM_LIT:1> ] . encode ( '<STR_LIT>' ) ) <EOL> index += <NUM_LIT:1> <EOL> return returnMat , classLabelVector <EOL> def autoNorm ( dataSet ) : <EOL> minVals = dataSet . min ( <NUM_LIT:0> ) <EOL> maxVals = dataSet . max ( <NUM_LIT:0> ) <EOL> ranges = maxVals - minVals <EOL> normDataSet = zeros ( shape ( dataSet ) ) <EOL> m = dataSet . shape [ <NUM_LIT:0> ] <EOL> normDataSet = dataSet - tile ( minVals , ( m , <NUM_LIT:1> ) ) <EOL> normDataSet = normDataSet / tile ( ranges , ( m , <NUM_LIT:1> ) ) <EOL> return normDataSet , ranges , minVals <EOL> def listDirInMac ( path ) : <EOL> os_list = os . listdir ( path ) <EOL> for item in os_list : <EOL> if item . startswith ( '<STR_LIT:.>' ) and os . path . isfile ( os . path . join ( path , item ) ) : <EOL> os_list . remove ( item ) <EOL> return os_list <EOL> handwritingClassTest ( ) </s>
