@@ -531,123 +531,3 @@ def testDigits(kTup=('rbf', 10)):
531531 predict = kernelEval .T * multiply (labelSV , alphas [svInd ]) + b
532532 if sign (predict ) != sign (labelArr [i ]): errorCount += 1
533533 print ("the test error rate is: %f" % (float (errorCount ) / m ))
534-
535-
536- '''#######********************************
537- Non-Kernel VErsions below
538- ''' #######********************************
539-
540-
541- class optStructK :
542- def __init__ (self , dataMatIn , classLabels , C , toler ): # Initialize the structure with the parameters
543- self .X = dataMatIn
544- self .labelMat = classLabels
545- self .C = C
546- self .tol = toler
547- self .m = shape (dataMatIn )[0 ]
548- self .alphas = mat (zeros ((self .m , 1 )))
549- self .b = 0
550- self .eCache = mat (zeros ((self .m , 2 ))) # first column is valid flag
551-
552-
553- def calcEkK (oS , k ):
554- fXk = float (multiply (oS .alphas , oS .labelMat ).T * (oS .X * oS .X [k , :].T )) + oS .b
555- Ek = fXk - float (oS .labelMat [k ])
556- return Ek
557-
558-
559- def selectJK (i , oS , Ei ): # this is the second choice -heurstic, and calcs Ej
560- maxK = - 1
561- maxDeltaE = 0
562- Ej = 0
563- oS .eCache [i ] = [1 , Ei ] # set valid #choose the alpha that gives the maximum delta E
564- validEcacheList = nonzero (oS .eCache [:, 0 ].A )[0 ]
565- if (len (validEcacheList )) > 1 :
566- for k in validEcacheList : # loop through valid Ecache values and find the one that maximizes delta E
567- if k == i : continue # don't calc for i, waste of time
568- Ek = calcEk (oS , k )
569- deltaE = abs (Ei - Ek )
570- if (deltaE > maxDeltaE ):
571- maxK = k
572- maxDeltaE = deltaE
573- Ej = Ek
574- return maxK , Ej
575- else : # in this case (first time around) we don't have any valid eCache values
576- j = selectJrand (i , oS .m )
577- Ej = calcEk (oS , j )
578- return j , Ej
579-
580-
581- def updateEkK (oS , k ): # after any alpha has changed update the new value in the cache
582- Ek = calcEk (oS , k )
583- oS .eCache [k ] = [1 , Ek ]
584-
585-
586- def innerLK (i , oS ):
587- Ei = calcEk (oS , i )
588- if ((oS .labelMat [i ] * Ei < - oS .tol ) and (oS .alphas [i ] < oS .C )) or (
589- (oS .labelMat [i ] * Ei > oS .tol ) and (oS .alphas [i ] > 0 )):
590- j , Ej = selectJ (i , oS , Ei ) # this has been changed from selectJrand
591- alphaIold = oS .alphas [i ].copy ()
592- alphaJold = oS .alphas [j ].copy ()
593- if (oS .labelMat [i ] != oS .labelMat [j ]):
594- L = max (0 , oS .alphas [j ] - oS .alphas [i ])
595- H = min (oS .C , oS .C + oS .alphas [j ] - oS .alphas [i ])
596- else :
597- L = max (0 , oS .alphas [j ] + oS .alphas [i ] - oS .C )
598- H = min (oS .C , oS .alphas [j ] + oS .alphas [i ])
599- if L == H :
600- print ("L==H" )
601- return 0
602- eta = 2.0 * oS .X [i , :] * oS .X [j , :].T - oS .X [i , :] * oS .X [i , :].T - oS .X [j , :] * oS .X [j , :].T
603- if eta >= 0 :
604- print ("eta>=0" )
605- return 0
606- oS .alphas [j ] -= oS .labelMat [j ] * (Ei - Ej ) / eta
607- oS .alphas [j ] = clipAlpha (oS .alphas [j ], H , L )
608- updateEk (oS , j ) # added this for the Ecache
609- if (abs (oS .alphas [j ] - alphaJold ) < 0.00001 ):
610- print ("j not moving enough" )
611- return 0
612- oS .alphas [i ] += oS .labelMat [j ] * oS .labelMat [i ] * (alphaJold - oS .alphas [j ]) # update i by the same amount as j
613- updateEk (oS , i ) # added this for the Ecache #the update is in the oppostie direction
614- b1 = oS .b - Ei - oS .labelMat [i ] * (oS .alphas [i ] - alphaIold ) * oS .X [i , :] * oS .X [i , :].T - oS .labelMat [j ] * (
615- oS .alphas [j ] - alphaJold ) * oS .X [i , :] * oS .X [j , :].T
616- b2 = oS .b - Ej - oS .labelMat [i ] * (oS .alphas [i ] - alphaIold ) * oS .X [i , :] * oS .X [j , :].T - oS .labelMat [j ] * (
617- oS .alphas [j ] - alphaJold ) * oS .X [j , :] * oS .X [j , :].T
618- if (0 < oS .alphas [i ]) and (oS .C > oS .alphas [i ]):
619- oS .b = b1
620- elif (0 < oS .alphas [j ]) and (oS .C > oS .alphas [j ]):
621- oS .b = b2
622- else :
623- oS .b = (b1 + b2 ) / 2.0
624- return 1
625- else :
626- return 0
627-
628-
629- def smoPK (dataMatIn , classLabels , C , toler , maxIter ): # full Platt SMO
630- oS = optStruct (mat (dataMatIn ), mat (classLabels ).transpose (), C , toler )
631- iter = 0
632- entireSet = True
633- alphaPairsChanged = 0
634- while (iter < maxIter ) and ((alphaPairsChanged > 0 ) or (entireSet )):
635- alphaPairsChanged = 0
636- if entireSet : # go over all
637- for i in range (oS .m ):
638- alphaPairsChanged += innerL (i , oS )
639- print ("fullSet, iter: %d i:%d, pairs changed %d" % (iter , i , alphaPairsChanged ))
640- iter += 1
641- else : # go over non-bound (railed) alphas
642- nonBoundIs = nonzero ((oS .alphas .A > 0 ) * (oS .alphas .A < C ))[0 ]
643- for i in nonBoundIs :
644- alphaPairsChanged += innerL (i , oS )
645- print ("non-bound, iter: %d i:%d, pairs changed %d" % (iter , i , alphaPairsChanged ))
646- iter += 1
647- if entireSet :
648- entireSet = False # toggle entire set loop
649- elif (alphaPairsChanged == 0 ):
650- entireSet = True
651- print ("iteration number: %d" % iter )
652- return oS .b , oS .alphas
653-
0 commit comments