<s> import os <EOL> from numpy import * <EOL> from time import sleep <EOL> def loadDataSet ( fileName ) : <EOL> dataMat = [ ] ; <EOL> labelMat = [ ] <EOL> fr = open ( fileName ) <EOL> for line in fr . readlines ( ) : <EOL> lineArr = line . strip ( ) . split ( '<STR_LIT:\t>' ) <EOL> dataMat . append ( [ float ( lineArr [ <NUM_LIT:0> ] ) , float ( lineArr [ <NUM_LIT:1> ] ) ] ) <EOL> labelMat . append ( float ( lineArr [ <NUM_LIT:2> ] ) ) <EOL> return dataMat , labelMat <EOL> def selectJrand ( i , m ) : <EOL> j = i <EOL> while ( j == i ) : <EOL> j = int ( random . uniform ( <NUM_LIT:0> , m ) ) <EOL> return j <EOL> def clipAlpha ( aj , H , L ) : <EOL> if aj > H : <EOL> aj = H <EOL> if L > aj : <EOL> aj = L <EOL> return aj <EOL> def smoSimple ( dataMatIn , classLabels , C , toler , maxIter ) : <EOL> dataMatrix = mat ( dataMatIn ) ; <EOL> labelMat = mat ( classLabels ) . transpose ( ) <EOL> b = <NUM_LIT:0> ; <EOL> m , n = shape ( dataMatrix ) <EOL> alphas = mat ( zeros ( ( m , <NUM_LIT:1> ) ) ) <EOL> iter = <NUM_LIT:0> <EOL> while ( iter < maxIter ) : <EOL> alphaPairsChanged = <NUM_LIT:0> <EOL> for i in range ( m ) : <EOL> fXi = float ( multiply ( alphas , labelMat ) . T * ( dataMatrix * dataMatrix [ i , : ] . T ) ) + b <EOL> Ei = fXi - float ( labelMat [ i ] ) <EOL> if ( ( labelMat [ i ] * Ei < - toler ) and ( alphas [ i ] < C ) ) or ( ( labelMat [ i ] * Ei > toler ) and ( alphas [ i ] > <NUM_LIT:0> ) ) : <EOL> j = selectJrand ( i , m ) <EOL> fXj = float ( multiply ( alphas , labelMat ) . T * ( dataMatrix * dataMatrix [ j , : ] . T ) ) + b <EOL> Ej = fXj - float ( labelMat [ j ] ) <EOL> alphaIold = alphas [ i ] . copy ( ) ; <EOL> alphaJold = alphas [ j ] . copy ( ) ; <EOL> if ( labelMat [ i ] != labelMat [ j ] ) : <EOL> L = max ( <NUM_LIT:0> , alphas [ j ] - alphas [ i ] ) <EOL> H = min ( C , C + alphas [ j ] - alphas [ i ] ) <EOL> else : <EOL> L = max ( <NUM_LIT:0> , alphas [ j ] + alphas [ i ] - C ) <EOL> H = min ( C , alphas [ j ] + alphas [ i ] ) <EOL> if L == H : print "<STR_LIT>" ; continue <EOL> eta = <NUM_LIT> * dataMatrix [ i , : ] * dataMatrix [ j , : ] . T - dataMatrix [ i , : ] * dataMatrix [ i , : ] . T - dataMatrix [ j , : ] * dataMatrix [ j , : ] . T <EOL> if eta >= <NUM_LIT:0> : print "<STR_LIT>" ; continue <EOL> alphas [ j ] -= labelMat [ j ] * ( Ei - Ej ) / eta <EOL> alphas [ j ] = clipAlpha ( alphas [ j ] , H , L ) <EOL> if ( abs ( alphas [ j ] - alphaJold ) < <NUM_LIT> ) : print "<STR_LIT>" ; continue <EOL> alphas [ i ] += labelMat [ j ] * labelMat [ i ] * ( alphaJold - alphas [ j ] ) <EOL> b1 = b - Ei - labelMat [ i ] * ( alphas [ i ] - alphaIold ) * dataMatrix [ i , : ] * dataMatrix [ i , : ] . T - labelMat [ j ] * ( alphas [ j ] - alphaJold ) * dataMatrix [ i , : ] * dataMatrix [ j , : ] . T <EOL> b2 = b - Ej - labelMat [ i ] * ( alphas [ i ] - alphaIold ) * dataMatrix [ i , : ] * dataMatrix [ j , : ] . T - labelMat [ j ] * ( alphas [ j ] - alphaJold ) * dataMatrix [ j , : ] * dataMatrix [ j , : ] . T <EOL> if ( <NUM_LIT:0> < alphas [ i ] ) and ( C > alphas [ i ] ) : <EOL> b = b1 <EOL> elif ( <NUM_LIT:0> < alphas [ j ] ) and ( C > alphas [ j ] ) : <EOL> b = b2 <EOL> else : <EOL> b = ( b1 + b2 ) / <NUM_LIT> <EOL> alphaPairsChanged += <NUM_LIT:1> <EOL> print "<STR_LIT>" % ( iter , i , alphaPairsChanged ) <EOL> if ( alphaPairsChanged == <NUM_LIT:0> ) : <EOL> iter += <NUM_LIT:1> <EOL> else : <EOL> iter = <NUM_LIT:0> <EOL> print "<STR_LIT>" % iter <EOL> return b , alphas <EOL> def kernelTrans ( X , A , kTup ) : <EOL> m , n = shape ( X ) <EOL> K = mat ( zeros ( ( m , <NUM_LIT:1> ) ) ) <EOL> if kTup [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> K = X * A . T <EOL> elif kTup [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> for j in range ( m ) : <EOL> deltaRow = X [ j , : ] - A <EOL> K [ j ] = deltaRow * deltaRow . T <EOL> K = exp ( K / ( - <NUM_LIT:1> * kTup [ <NUM_LIT:1> ] ** <NUM_LIT:2> ) ) <EOL> else : <EOL> raise NameError ( '<STR_LIT>' ) <EOL> return K <EOL> class optStruct : <EOL> def __init__ ( self , dataMatIn , classLabels , C , toler , kTup ) : <EOL> self . X = dataMatIn <EOL> self . labelMat = classLabels <EOL> self . C = C <EOL> self . tol = toler <EOL> self . m = shape ( dataMatIn ) [ <NUM_LIT:0> ] <EOL> self . alphas = mat ( zeros ( ( self . m , <NUM_LIT:1> ) ) ) <EOL> self . b = <NUM_LIT:0> <EOL> self . eCache = mat ( zeros ( ( self . m , <NUM_LIT:2> ) ) ) <EOL> self . K = mat ( zeros ( ( self . m , self . m ) ) ) <EOL> for i in range ( self . m ) : <EOL> self . K [ : , i ] = kernelTrans ( self . X , self . X [ i , : ] , kTup ) <EOL> def calcEk ( oS , k ) : <EOL> fXk = float ( multiply ( oS . alphas , oS . labelMat ) . T * oS . K [ : , k ] + oS . b ) <EOL> Ek = fXk - float ( oS . labelMat [ k ] ) <EOL> return Ek <EOL> def selectJ ( i , oS , Ei ) : <EOL> maxK = - <NUM_LIT:1> ; <EOL> maxDeltaE = <NUM_LIT:0> ; <EOL> Ej = <NUM_LIT:0> <EOL> oS . eCache [ i ] = [ <NUM_LIT:1> , Ei ] <EOL> validEcacheList = nonzero ( oS . eCache [ : , <NUM_LIT:0> ] . A ) [ <NUM_LIT:0> ] <EOL> if ( len ( validEcacheList ) ) > <NUM_LIT:1> : <EOL> for k in validEcacheList : <EOL> if k == i : continue <EOL> Ek = calcEk ( oS , k ) <EOL> deltaE = abs ( Ei - Ek ) <EOL> if ( deltaE > maxDeltaE ) : <EOL> maxK = k ; <EOL> maxDeltaE = deltaE ; <EOL> Ej = Ek <EOL> return maxK , Ej <EOL> else : <EOL> j = selectJrand ( i , oS . m ) <EOL> Ej = calcEk ( oS , j ) <EOL> return j , Ej <EOL> def updateEk ( oS , k ) : <EOL> Ek = calcEk ( oS , k ) <EOL> oS . eCache [ k ] = [ <NUM_LIT:1> , Ek ] <EOL> def innerL ( i , oS ) : <EOL> Ei = calcEk ( oS , i ) <EOL> if ( ( oS . labelMat [ i ] * Ei < - oS . tol ) and ( oS . alphas [ i ] < oS . C ) ) or ( <EOL> ( oS . labelMat [ i ] * Ei > oS . tol ) and ( oS . alphas [ i ] > <NUM_LIT:0> ) ) : <EOL> j , Ej = selectJ ( i , oS , Ei ) <EOL> alphaIold = oS . alphas [ i ] . copy ( ) ; <EOL> alphaJold = oS . alphas [ j ] . copy ( ) ; <EOL> if ( oS . labelMat [ i ] != oS . labelMat [ j ] ) : <EOL> L = max ( <NUM_LIT:0> , oS . alphas [ j ] - oS . alphas [ i ] ) <EOL> H = min ( oS . C , oS . C + oS . alphas [ j ] - oS . alphas [ i ] ) <EOL> else : <EOL> L = max ( <NUM_LIT:0> , oS . alphas [ j ] + oS . alphas [ i ] - oS . C ) <EOL> H = min ( oS . C , oS . alphas [ j ] + oS . alphas [ i ] ) <EOL> if L == H : print "<STR_LIT>" ; return <NUM_LIT:0> <EOL> eta = <NUM_LIT> * oS . K [ i , j ] - oS . K [ i , i ] - oS . K [ j , j ] <EOL> if eta >= <NUM_LIT:0> : print "<STR_LIT>" ; return <NUM_LIT:0> <EOL> oS . alphas [ j ] -= oS . labelMat [ j ] * ( Ei - Ej ) / eta <EOL> oS . alphas [ j ] = clipAlpha ( oS . alphas [ j ] , H , L ) <EOL> updateEk ( oS , j ) <EOL> if ( abs ( oS . alphas [ j ] - alphaJold ) < <NUM_LIT> ) : print "<STR_LIT>" ; return <NUM_LIT:0> <EOL> oS . alphas [ i ] += oS . labelMat [ j ] * oS . labelMat [ i ] * ( alphaJold - oS . alphas [ j ] ) <EOL> updateEk ( oS , i ) <EOL> b1 = oS . b - Ei - oS . labelMat [ i ] * ( oS . alphas [ i ] - alphaIold ) * oS . K [ i , i ] - oS . labelMat [ j ] * ( <EOL> oS . alphas [ j ] - alphaJold ) * oS . K [ i , j ] <EOL> b2 = oS . b - Ej - oS . labelMat [ i ] * ( oS . alphas [ i ] - alphaIold ) * oS . K [ i , j ] - oS . labelMat [ j ] * ( <EOL> oS . alphas [ j ] - alphaJold ) * oS . K [ j , j ] <EOL> if ( <NUM_LIT:0> < oS . alphas [ i ] ) and ( oS . C > oS . alphas [ i ] ) : <EOL> oS . b = b1 <EOL> elif ( <NUM_LIT:0> < oS . alphas [ j ] ) and ( oS . C > oS . alphas [ j ] ) : <EOL> oS . b = b2 <EOL> else : <EOL> oS . b = ( b1 + b2 ) / <NUM_LIT> <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def smoP ( dataMatIn , classLabels , C , toler , maxIter , kTup = ( '<STR_LIT>' , <NUM_LIT:0> ) ) : <EOL> oS = optStruct ( mat ( dataMatIn ) , mat ( classLabels ) . transpose ( ) , C , toler , kTup ) <EOL> iter = <NUM_LIT:0> <EOL> entireSet = True ; <EOL> alphaPairsChanged = <NUM_LIT:0> <EOL> while ( iter < maxIter ) and ( ( alphaPairsChanged > <NUM_LIT:0> ) or ( entireSet ) ) : <EOL> alphaPairsChanged = <NUM_LIT:0> <EOL> if entireSet : <EOL> for i in range ( oS . m ) : <EOL> alphaPairsChanged += innerL ( i , oS ) <EOL> print "<STR_LIT>" % ( iter , i , alphaPairsChanged ) <EOL> iter += <NUM_LIT:1> <EOL> else : <EOL> nonBoundIs = nonzero ( ( oS . alphas . A > <NUM_LIT:0> ) * ( oS . alphas . A < C ) ) [ <NUM_LIT:0> ] <EOL> for i in nonBoundIs : <EOL> alphaPairsChanged += innerL ( i , oS ) <EOL> print "<STR_LIT>" % ( iter , i , alphaPairsChanged ) <EOL> iter += <NUM_LIT:1> <EOL> if entireSet : <EOL> entireSet = False <EOL> elif ( alphaPairsChanged == <NUM_LIT:0> ) : <EOL> entireSet = True <EOL> print "<STR_LIT>" % iter <EOL> return oS . b , oS . alphas <EOL> def calcWs ( alphas , dataArr , classLabels ) : <EOL> X = mat ( dataArr ) ; <EOL> labelMat = mat ( classLabels ) . transpose ( ) <EOL> m , n = shape ( X ) <EOL> w = zeros ( ( n , <NUM_LIT:1> ) ) <EOL> for i in range ( m ) : <EOL> w += multiply ( alphas [ i ] * labelMat [ i ] , X [ i , : ] . T ) <EOL> return w <EOL> def testRbf ( k1 = <NUM_LIT> ) : <EOL> dataArr , labelArr = loadDataSet ( '<STR_LIT>' ) <EOL> b , alphas = smoP ( dataArr , labelArr , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , ( '<STR_LIT>' , k1 ) ) <EOL> datMat = mat ( dataArr ) ; <EOL> labelMat = mat ( labelArr ) . transpose ( ) <EOL> svInd = nonzero ( alphas . A > <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> sVs = datMat [ svInd ] <EOL> labelSV = labelMat [ svInd ] ; <EOL> print "<STR_LIT>" % shape ( sVs ) [ <NUM_LIT:0> ] <EOL> m , n = shape ( datMat ) <EOL> errorCount = <NUM_LIT:0> <EOL> for i in range ( m ) : <EOL> kernelEval = kernelTrans ( sVs , datMat [ i , : ] , ( '<STR_LIT>' , k1 ) ) <EOL> predict = kernelEval . T * multiply ( labelSV , alphas [ svInd ] ) + b <EOL> if sign ( predict ) != sign ( labelArr [ i ] ) : errorCount += <NUM_LIT:1> <EOL> print "<STR_LIT>" % ( float ( errorCount ) / m ) <EOL> dataArr , labelArr = loadDataSet ( '<STR_LIT>' ) <EOL> errorCount = <NUM_LIT:0> <EOL> datMat = mat ( dataArr ) ; <EOL> labelMat = mat ( labelArr ) . transpose ( ) <EOL> m , n = shape ( datMat ) <EOL> for i in range ( m ) : <EOL> kernelEval = kernelTrans ( sVs , datMat [ i , : ] , ( '<STR_LIT>' , k1 ) ) <EOL> predict = kernelEval . T * multiply ( labelSV , alphas [ svInd ] ) + b <EOL> if sign ( predict ) != sign ( labelArr [ i ] ) : errorCount += <NUM_LIT:1> <EOL> print "<STR_LIT>" % ( float ( errorCount ) / m ) <EOL> def img2vector ( filename ) : <EOL> returnVect = zeros ( ( <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> fr = open ( filename ) <EOL> for i in range ( <NUM_LIT:32> ) : <EOL> lineStr = fr . readline ( ) <EOL> for j in range ( <NUM_LIT:32> ) : <EOL> returnVect [ <NUM_LIT:0> , <NUM_LIT:32> * i + j ] = int ( lineStr [ j ] ) <EOL> return returnVect <EOL> def loadImages ( dirName ) : <EOL> from os import listdir <EOL> hwLabels = [ ] <EOL> trainingFileList = listDirInMac ( dirName ) <EOL> m = len ( trainingFileList ) <EOL> trainingMat = zeros ( ( m , <NUM_LIT> ) ) <EOL> for i in range ( m ) : <EOL> fileNameStr = trainingFileList [ i ] <EOL> fileStr = fileNameStr . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> classNumStr = int ( fileStr . split ( '<STR_LIT:_>' ) [ <NUM_LIT:0> ] ) <EOL> if classNumStr == <NUM_LIT:9> : <EOL> hwLabels . append ( - <NUM_LIT:1> ) <EOL> else : <EOL> hwLabels . append ( <NUM_LIT:1> ) <EOL> trainingMat [ i , : ] = img2vector ( '<STR_LIT>' % ( dirName , fileNameStr ) ) <EOL> return trainingMat , hwLabels <EOL> def testDigits ( kTup = ( '<STR_LIT>' , <NUM_LIT:10> ) ) : <EOL> dataArr , labelArr = loadImages ( '<STR_LIT>' ) <EOL> b , alphas = smoP ( dataArr , labelArr , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , kTup ) <EOL> datMat = mat ( dataArr ) ; <EOL> labelMat = mat ( labelArr ) . transpose ( ) <EOL> svInd = nonzero ( alphas . A > <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> sVs = datMat [ svInd ] <EOL> labelSV = labelMat [ svInd ] ; <EOL> print "<STR_LIT>" % shape ( sVs ) [ <NUM_LIT:0> ] <EOL> m , n = shape ( datMat ) <EOL> errorCount = <NUM_LIT:0> <EOL> for i in range ( m ) : <EOL> kernelEval = kernelTrans ( sVs , datMat [ i , : ] , kTup ) <EOL> predict = kernelEval . T * multiply ( labelSV , alphas [ svInd ] ) + b <EOL> if sign ( predict ) != sign ( labelArr [ i ] ) : errorCount += <NUM_LIT:1> <EOL> print "<STR_LIT>" % ( float ( errorCount ) / m ) <EOL> dataArr , labelArr = loadImages ( '<STR_LIT>' ) <EOL> errorCount = <NUM_LIT:0> <EOL> datMat = mat ( dataArr ) ; <EOL> labelMat = mat ( labelArr ) . transpose ( ) <EOL> m , n = shape ( datMat ) <EOL> for i in range ( m ) : <EOL> kernelEval = kernelTrans ( sVs , datMat [ i , : ] , kTup ) <EOL> predict = kernelEval . T * multiply ( labelSV , alphas [ svInd ] ) + b <EOL> if sign ( predict ) != sign ( labelArr [ i ] ) : errorCount += <NUM_LIT:1> <EOL> print "<STR_LIT>" % ( float ( errorCount ) / m ) <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> '''<STR_LIT>''' <EOL> class optStructK : <EOL> def __init__ ( self , dataMatIn , classLabels , C , toler ) : <EOL> self . X = dataMatIn <EOL> self . labelMat = classLabels <EOL> self . C = C <EOL> self . tol = toler <EOL> self . m = shape ( dataMatIn ) [ <NUM_LIT:0> ] <EOL> self . alphas = mat ( zeros ( ( self . m , <NUM_LIT:1> ) ) ) <EOL> self . b = <NUM_LIT:0> <EOL> self . eCache = mat ( zeros ( ( self . m , <NUM_LIT:2> ) ) ) <EOL> def calcEkK ( oS , k ) : <EOL> fXk = float ( multiply ( oS . alphas , oS . labelMat ) . T * ( oS . X * oS . X [ k , : ] . T ) ) + oS . b <EOL> Ek = fXk - float ( oS . labelMat [ k ] ) <EOL> return Ek <EOL> def selectJK ( i , oS , Ei ) : <EOL> maxK = - <NUM_LIT:1> ; <EOL> maxDeltaE = <NUM_LIT:0> ; <EOL> Ej = <NUM_LIT:0> <EOL> oS . eCache [ i ] = [ <NUM_LIT:1> , Ei ] <EOL> validEcacheList = nonzero ( oS . eCache [ : , <NUM_LIT:0> ] . A ) [ <NUM_LIT:0> ] <EOL> if ( len ( validEcacheList ) ) > <NUM_LIT:1> : <EOL> for k in validEcacheList : <EOL> if k == i : continue <EOL> Ek = calcEk ( oS , k ) <EOL> deltaE = abs ( Ei - Ek ) <EOL> if ( deltaE > maxDeltaE ) : <EOL> maxK = k ; <EOL> maxDeltaE = deltaE ; <EOL> Ej = Ek <EOL> return maxK , Ej <EOL> else : <EOL> j = selectJrand ( i , oS . m ) <EOL> Ej = calcEk ( oS , j ) <EOL> return j , Ej <EOL> def updateEkK ( oS , k ) : <EOL> Ek = calcEk ( oS , k ) <EOL> oS . eCache [ k ] = [ <NUM_LIT:1> , Ek ] <EOL> def innerLK ( i , oS ) : <EOL> Ei = calcEk ( oS , i ) <EOL> if ( ( oS . labelMat [ i ] * Ei < - oS . tol ) and ( oS . alphas [ i ] < oS . C ) ) or ( <EOL> ( oS . labelMat [ i ] * Ei > oS . tol ) and ( oS . alphas [ i ] > <NUM_LIT:0> ) ) : <EOL> j , Ej = selectJ ( i , oS , Ei ) <EOL> alphaIold = oS . alphas [ i ] . copy ( ) ; <EOL> alphaJold = oS . alphas [ j ] . copy ( ) ; <EOL> if ( oS . labelMat [ i ] != oS . labelMat [ j ] ) : <EOL> L = max ( <NUM_LIT:0> , oS . alphas [ j ] - oS . alphas [ i ] ) <EOL> H = min ( oS . C , oS . C + oS . alphas [ j ] - oS . alphas [ i ] ) <EOL> else : <EOL> L = max ( <NUM_LIT:0> , oS . alphas [ j ] + oS . alphas [ i ] - oS . C ) <EOL> H = min ( oS . C , oS . alphas [ j ] + oS . alphas [ i ] ) <EOL> if L == H : print "<STR_LIT>" ; return <NUM_LIT:0> <EOL> eta = <NUM_LIT> * oS . X [ i , : ] * oS . X [ j , : ] . T - oS . X [ i , : ] * oS . X [ i , : ] . T - oS . X [ j , : ] * oS . X [ j , : ] . T <EOL> if eta >= <NUM_LIT:0> : print "<STR_LIT>" ; return <NUM_LIT:0> <EOL> oS . alphas [ j ] -= oS . labelMat [ j ] * ( Ei - Ej ) / eta <EOL> oS . alphas [ j ] = clipAlpha ( oS . alphas [ j ] , H , L ) <EOL> updateEk ( oS , j ) <EOL> if ( abs ( oS . alphas [ j ] - alphaJold ) < <NUM_LIT> ) : print "<STR_LIT>" ; return <NUM_LIT:0> <EOL> oS . alphas [ i ] += oS . labelMat [ j ] * oS . labelMat [ i ] * ( alphaJold - oS . alphas [ j ] ) <EOL> updateEk ( oS , i ) <EOL> b1 = oS . b - Ei - oS . labelMat [ i ] * ( oS . alphas [ i ] - alphaIold ) * oS . X [ i , : ] * oS . X [ i , : ] . T - oS . labelMat [ j ] * ( <EOL> oS . alphas [ j ] - alphaJold ) * oS . X [ i , : ] * oS . X [ j , : ] . T <EOL> b2 = oS . b - Ej - oS . labelMat [ i ] * ( oS . alphas [ i ] - alphaIold ) * oS . X [ i , : ] * oS . X [ j , : ] . T - oS . labelMat [ j ] * ( <EOL> oS . alphas [ j ] - alphaJold ) * oS . X [ j , : ] * oS . X [ j , : ] . T <EOL> if ( <NUM_LIT:0> < oS . alphas [ i ] ) and ( oS . C > oS . alphas [ i ] ) : <EOL> oS . b = b1 <EOL> elif ( <NUM_LIT:0> < oS . alphas [ j ] ) and ( oS . C > oS . alphas [ j ] ) : <EOL> oS . b = b2 <EOL> else : <EOL> oS . b = ( b1 + b2 ) / <NUM_LIT> <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def smoPK ( dataMatIn , classLabels , C , toler , maxIter ) : <EOL> oS = optStruct ( mat ( dataMatIn ) , mat ( classLabels ) . transpose ( ) , C , toler ) <EOL> iter = <NUM_LIT:0> <EOL> entireSet = True ; <EOL> alphaPairsChanged = <NUM_LIT:0> <EOL> while ( iter < maxIter ) and ( ( alphaPairsChanged > <NUM_LIT:0> ) or ( entireSet ) ) : <EOL> alphaPairsChanged = <NUM_LIT:0> <EOL> if entireSet : <EOL> for i in range ( oS . m ) : <EOL> alphaPairsChanged += innerL ( i , oS ) <EOL> print "<STR_LIT>" % ( iter , i , alphaPairsChanged ) <EOL> iter += <NUM_LIT:1> <EOL> else : <EOL> nonBoundIs = nonzero ( ( oS . alphas . A > <NUM_LIT:0> ) * ( oS . alphas . A < C ) ) [ <NUM_LIT:0> ] <EOL> for i in nonBoundIs : <EOL> alphaPairsChanged += innerL ( i , oS ) <EOL> print "<STR_LIT>" % ( iter , i , alphaPairsChanged ) <EOL> iter += <NUM_LIT:1> <EOL> if entireSet : <EOL> entireSet = False <EOL> elif ( alphaPairsChanged == <NUM_LIT:0> ) : <EOL> entireSet = True <EOL> print "<STR_LIT>" % iter <EOL> return oS . b , oS . alphas <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> testDigits ( ( '<STR_LIT>' , <NUM_LIT:20> ) ) </s>
