标签:很多 ide loaddata sleep lines mos led ice maximum
SVM有很多种实现,但是本章只关注其中最流行的一种实现,即序列最小化(SMO)算法
在此之后,我们将介绍如何使用一种称为核函数的方式将SVM扩展到更多的数据集上
基于最大间隔的分割数据
优点:泛化错误率低,计算开销不大,结果易解释
缺点:对参数调节和核函数的选择敏感,原始分类器不加修改仅适用于处理二类问题
适用数据类型:数值型和标称型数据
寻找最大间隔:
分割超平面的形式可以写成W^T *x+b,要计算点A到分割超平面的距离,就必须给出点
到分割面的法线或垂线的长度,该值为|w^T+b|/||w||.这里的常数b类似于Logistic回
归中的结局w0 .
SVM的类别标签采用的是1和-1,而不是0和1,这是为什么呢?
这是由于-1和+1仅仅相差一个符号,方便数学上的处理,实质上是和目标函数的选取(算法的判别函数)有关。
当计算数据点到分割面的距离并确定分割面的放置位置时,间隔通过label*(W^T *x+b)来计算,这是就能体
现出-1和+1的好处了。即只要判断正确,判别条件总是大于1.
至此,一切都很完美,但是这里有个假设:数据必须100%线性可分。目前为止,物品们直到几乎所有数据
都不那么“干净”。这时,我们就可以通过引入所谓的松弛i按量,来允许有些数据点可以处于分割面的错误一侧。
SVM应用的一般框架
SVM的一般流程
(1)收集数据:可以适用任意方法
(2)准备数据:需要数值型数据
(3)分析数据:有助于可视化分割 超平面
(4)训练算法:SVM的大部分时间都源自训练,该过程主要实现两个参数的调优
(5)测试算法:十分简单的计算过程就可以实现
(6)使用算法:几乎所有分类问题都可以使用SVM,值得一提的是,SVM本身是一个二类分类器,对多类问题
应用SVM需要对代码做一些修改。
SMO表示序列的最小优化,这些小优化问题往往容易为蟹,并且对他们进行顺序求解的结果将与他们作为整
体来求解的结果完全一致。在结果完全相同时,SMO算法的求解时间最短。
SMO算法的求解目标是求一系列的alpha和b,一旦求出alpha,就很容易计算出权重向量w并得到分割超平面
SMO算法的工作原理是:每次循环中选择两个alpha进行优化处理。一旦找到一对合适的alpha,那么就增大
其中一个同时减小另一个。这里所谓的“合适”就是指两个alpha必须符合一定的条件,条件之一就是这两个
alpha必须要在间隔边界之外,而其第二个条件则是这两个alpha还没有进行过区间化或者不再边界上。
1 from numpy import * 2 from time import sleep 3 4 def loadDataSet(fileName): 5 dataMat = []; labelMat = [] 6 fr = open(fileName) 7 for line in fr.readlines(): 8 lineArr = line.strip().split(‘\t‘) 9 dataMat.append([float(lineArr[0]), float(lineArr[1])]) 10 labelMat.append(float(lineArr[2])) 11 return dataMat,labelMat 12 13 #i是第一个alpha的下标,m是所有alpha的数目。只要函数值不等于输入值i,函数就会进行随机选择。 14 def selectJrand(i,m): 15 j=i #we want to select any J not equal to i 16 while (j==i): 17 j = int(random.uniform(0,m)) 18 return j 19 20 #该函数用于调整大于H或小于L的alpha值。 21 def clipAlpha(aj,H,L): 22 if aj > H: 23 aj = H 24 if L > aj: 25 aj = L 26 return aj 27 28 ‘‘‘ 29 创建一个alpha向量并将其初始化为0向量 30 当迭代次数小于最大迭代次数时(外循环) 31 对数据集中的每个数据向量(内循环): 32 如果该数据向量可以被优化: 33 随机选择另外一个数据向量 34 同时优化这两个向量 35 如果两个向量都不能被优化,退出内循环 36 如果所有向量都没被优化,增加迭代数目,继续下一次循环 37 ‘‘‘ 38 def smoSimple(dataMatIn, classLabels, C, toler, maxIter): 39 dataMatrix = mat(dataMatIn); labelMat = mat(classLabels).transpose() 40 b = 0; m,n = shape(dataMatrix) 41 alphas = mat(zeros((m,1))) 42 iter = 0 43 while (iter < maxIter): 44 alphaPairsChanged = 0 45 for i in range(m): 46 fXi = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b 47 Ei = fXi - float(labelMat[i])#if checks if an example violates KKT conditions 48 if ((labelMat[i]*Ei < -toler) and (alphas[i] < C)) or ((labelMat[i]*Ei > toler) and (alphas[i] > 0)): 49 j = selectJrand(i,m) 50 fXj = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b 51 Ej = fXj - float(labelMat[j]) 52 alphaIold = alphas[i].copy(); alphaJold = alphas[j].copy(); 53 if (labelMat[i] != labelMat[j]): 54 L = max(0, alphas[j] - alphas[i]) 55 H = min(C, C + alphas[j] - alphas[i]) 56 else: 57 L = max(0, alphas[j] + alphas[i] - C) 58 H = min(C, alphas[j] + alphas[i]) 59 if L==H: print("L==H"); continue 60 eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T - dataMatrix[i,:]*dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T 61 if eta >= 0: print("eta>=0"); continue 62 alphas[j] -= labelMat[j]*(Ei - Ej)/eta 63 alphas[j] = clipAlpha(alphas[j],H,L) 64 if (abs(alphas[j] - alphaJold) < 0.00001): print ("j not moving enough"); continue 65 alphas[i] += labelMat[j]*labelMat[i]*(alphaJold - alphas[j])#update i by the same amount as j 66 #the update is in the oppostie direction 67 b1 = b - Ei- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T 68 b2 = b - Ej- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T 69 if (0 < alphas[i]) and (C > alphas[i]): b = b1 70 elif (0 < alphas[j]) and (C > alphas[j]): b = b2 71 else: b = (b1 + b2)/2.0 72 alphaPairsChanged += 1 73 print("iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged)) 74 if (alphaPairsChanged == 0): iter += 1 75 else: iter = 0 76 print ("iteration number: %d" % iter) 77 return b,alphas 78 79 def kernelTrans(X, A, kTup): #calc the kernel or transform data to a higher dimensional space 80 m,n = shape(X) 81 K = mat(zeros((m,1))) 82 if kTup[0]==‘lin‘: K = X * A.T #linear kernel 83 elif kTup[0]==‘rbf‘: 84 for j in range(m): 85 deltaRow = X[j,:] - A 86 K[j] = deltaRow*deltaRow.T 87 K = exp(K/(-1*kTup[1]**2)) #divide in NumPy is element-wise not matrix like Matlab 88 else: raise NameError(‘Houston We Have a Problem -- 89 That Kernel is not recognized‘) 90 return K 91 92 #完整版的SMO的支持函数 93 class optStruct: 94 def __init__(self, dataMatIn, classLabels, C, toler, kTup): # Initialize the structure with the parameters 95 self.X = dataMatIn 96 self.labelMat = classLabels 97 self.C = C 98 self.tol = toler 99 self.m = shape(dataMatIn)[0] 100 self.alphas = mat(zeros((self.m, 1))) 101 self.b = 0 102 self.eCache = mat(zeros((self.m, 2))) # first column is valid flag 103 self.K = mat(zeros((self.m, self.m))) 104 for i in range(self.m): 105 self.K[:, i] = kernelTrans(self.X, self.X[i, :], kTup) 106 107 #误差缓存 108 def calcEk(oS, k): 109 fXk = float(multiply(oS.alphas, oS.labelMat).T * oS.K[:, k] + oS.b) 110 Ek = fXk - float(oS.labelMat[k]) 111 return Ek 112 113 #内循环中的启发式方法 114 def selectJ(i, oS, Ei): # this is the second choice -heurstic, and calcs Ej 115 maxK = -1; 116 maxDeltaE = 0; 117 Ej = 0 118 oS.eCache[i] = [1, Ei] # set valid #choose the alpha that gives the maximum delta E 119 validEcacheList = nonzero(oS.eCache[:, 0].A)[0] 120 if (len(validEcacheList)) > 1: 121 for k in validEcacheList: # loop through valid Ecache values and find the one that maximizes delta E 122 if k == i: continue # don‘t calc for i, waste of time 123 Ek = calcEk(oS, k) 124 deltaE = abs(Ei - Ek) 125 #选择具有最大步长的j 126 if (deltaE > maxDeltaE): 127 maxK = k; 128 maxDeltaE = deltaE; 129 Ej = Ek 130 return maxK, Ej 131 else: # in this case (first time around) we don‘t have any valid eCache values 132 j = selectJrand(i, oS.m) 133 Ej = calcEk(oS, j) 134 return j, Ej 135 136 137 def updateEk(oS, k): # after any alpha has changed update the new value in the cache 138 Ek = calcEk(oS, k) 139 oS.eCache[k] = [1, Ek] 140 141 def innerL(i, oS): 142 Ei = calcEk(oS, i) 143 if ((oS.labelMat[i]*Ei < -oS.tol) and (oS.alphas[i] < oS.C)) or ((oS.labelMat[i]*Ei > oS.tol) and (oS.alphas[i] > 0)): 144 j,Ej = selectJ(i, oS, Ei) #this has been changed from selectJrand 145 alphaIold = oS.alphas[i].copy(); alphaJold = oS.alphas[j].copy(); 146 if (oS.labelMat[i] != oS.labelMat[j]): 147 L = max(0, oS.alphas[j] - oS.alphas[i]) 148 H = min(oS.C, oS.C + oS.alphas[j] - oS.alphas[i]) 149 else: 150 L = max(0, oS.alphas[j] + oS.alphas[i] - oS.C) 151 H = min(oS.C, oS.alphas[j] + oS.alphas[i]) 152 if L==H: print("L==H"); return 0 153 eta = 2.0 * oS.K[i,j] - oS.K[i,i] - oS.K[j,j] #changed for kernel 154 if eta >= 0: print ("eta>=0"); return 0 155 oS.alphas[j] -= oS.labelMat[j]*(Ei - Ej)/eta 156 oS.alphas[j] = clipAlpha(oS.alphas[j],H,L) 157 updateEk(oS, j) #added this for the Ecache 158 if (abs(oS.alphas[j] - alphaJold) < 0.00001): print("j not moving enough"); return 0 159 oS.alphas[i] += oS.labelMat[j]*oS.labelMat[i]*(alphaJold - oS.alphas[j])#update i by the same amount as j 160 updateEk(oS, i) #added this for the Ecache #the update is in the oppostie direction 161 b1 = oS.b - Ei- oS.labelMat[i]*(oS.alphas[i]-alphaIold)*oS.K[i,i] - oS.labelMat[j]*(oS.alphas[j]-alphaJold)*oS.K[i,j] 162 b2 = oS.b - Ej- oS.labelMat[i]*(oS.alphas[i]-alphaIold)*oS.K[i,j]- oS.labelMat[j]*(oS.alphas[j]-alphaJold)*oS.K[j,j] 163 if (0 < oS.alphas[i]) and (oS.C > oS.alphas[i]): oS.b = b1 164 elif (0 < oS.alphas[j]) and (oS.C > oS.alphas[j]): oS.b = b2 165 else: oS.b = (b1 + b2)/2.0 166 return 1 167 else: return 0 168 169 def smoP(dataMatIn, classLabels, C, toler, maxIter,kTup=(‘lin‘, 0)): #full Platt SMO 170 oS = optStruct(mat(dataMatIn),mat(classLabels).transpose(),C,toler, kTup) 171 iter = 0 172 entireSet = True; alphaPairsChanged = 0 173 while (iter < maxIter) and ((alphaPairsChanged > 0) or (entireSet)): 174 alphaPairsChanged = 0 175 if entireSet: #go over all 176 for i in range(oS.m): 177 alphaPairsChanged += innerL(i,oS) 178 print("fullSet, iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged)) 179 iter += 1 180 else:#go over non-bound (railed) alphas 181 nonBoundIs = nonzero((oS.alphas.A > 0) * (oS.alphas.A < C))[0] 182 for i in nonBoundIs: 183 alphaPairsChanged += innerL(i,oS) 184 print("non-bound, iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged)) 185 iter += 1 186 if entireSet: entireSet = False #toggle entire set loop 187 elif (alphaPairsChanged == 0): entireSet = True 188 print("iteration number: %d" % iter) 189 return oS.b,oS.alphas 190 191 def calcWs(alphas,dataArr,classLabels): 192 X = mat(dataArr); labelMat = mat(classLabels).transpose() 193 m,n = shape(X) 194 w = zeros((n,1)) 195 for i in range(m): 196 w += multiply(alphas[i]*labelMat[i],X[i,:].T) 197 return w 198 199 200 201 dataArr,labelArr=loadDataSet(‘testSet.txt‘) 202 b,alphas=smoP(dataArr,labelArr,0.6,0.001,40) 203 ws=calcWs(alphas,dataArr,labelArr) 204 print(ws)
标签:很多 ide loaddata sleep lines mos led ice maximum
原文地址:https://www.cnblogs.com/zhibei/p/9353878.html