From 92336d8212d8cc0a356254c57ca02a41e84bcd2d Mon Sep 17 00:00:00 2001 From: geekidentity Date: Tue, 21 Mar 2017 23:18:35 +0800 Subject: [PATCH] SVM smoSimple --- src/python/06.SVM/svmMLiA.py | 118 +++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/python/06.SVM/svmMLiA.py diff --git a/src/python/06.SVM/svmMLiA.py b/src/python/06.SVM/svmMLiA.py new file mode 100644 index 00000000..8cc51a4a --- /dev/null +++ b/src/python/06.SVM/svmMLiA.py @@ -0,0 +1,118 @@ +""" +Created on Nov 4, 2010 +Update on 2017-03-21 +Chapter 5 source file for Machine Learing in Action +@author: Peter/geekidentity +""" +from numpy import * +from time import sleep + +def loadDataSet(fileName): + """ + 对文件进行逐行解析,从而得到第行的类标签和整个数据矩阵 + Args: + fileName: testSet.txt + + Returns: + 数据矩阵, 类标签 + """ + dataMat = []; labelMat = [] + fr = open(fileName) + for line in fr.readlines(): + lineArr = line.strip().split('\t') + dataMat.append([float(lineArr[0]), float(lineArr[1])]) + labelMat.append(float(lineArr[2])) + return dataMat,labelMat + +def selectJrand(i,m): + """ + 随机选择一个整数 + Args: + i: 第一个alpha的下标 + m: 所有alpha的数目 + + Returns: + + """ + j=i #we want to select any J not equal to i + while (j==i): + j = int(random.uniform(0,m)) + return j + +def clipAlpha(aj,H,L): + """ + 用于调整大于H或小于L的alpha值 + Args: + aj: + H: + L: + + Returns: + + """ + if aj > H: + aj = H + if L > aj: + aj = L + return aj + +def smoSimple(dataMatIn, classLabels, C, toler, maxIter): + """ + SVM SMO算法的简单实现: + 创建一个alpha向量并将其初始化为0向量 + 当迭代次数据小于最大迭代次数时(外循环) + 对数据集中的每个数据向量(内循环): + 如果该数据向量可以被优化: + 随机选择另外一个数据向量 + 同时优化这两个向量 + 如果两个向量都不能被优化,退出内循环 + 如果所有向量都没有被优化,增加迭代数目,继续下一次循环 + Args: + dataMatIn: 数据集 + classLabels: 类别标签 + C: 常数C + toler: 容错率 + maxIter: 退出前最大的循环次数 + + Returns: + + """ + dataMatrix = mat(dataMatIn); labelMat = mat(classLabels).transpose() + b = 0; m,n = shape(dataMatrix) + alphas = mat(zeros((m,1))) + iter = 0 + while (iter < maxIter): + alphaPairsChanged = 0 + for i in range(m): + fXi = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b + Ei = fXi - float(labelMat[i])#if checks if an example violates KKT conditions + if ((labelMat[i]*Ei < -toler) and (alphas[i] < C)) or ((labelMat[i]*Ei > toler) and (alphas[i] > 0)): + j = selectJrand(i,m) + fXj = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b + Ej = fXj - float(labelMat[j]) + alphaIold = alphas[i].copy(); alphaJold = alphas[j].copy() + if (labelMat[i] != labelMat[j]): + L = max(0, alphas[j] - alphas[i]) + H = min(C, C + alphas[j] - alphas[i]) + else: + L = max(0, alphas[j] + alphas[i] - C) + H = min(C, alphas[j] + alphas[i]) + if L==H: print("L==H"); continue + eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T - dataMatrix[i,:]*dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T + if eta >= 0: print("eta>=0"); continue + alphas[j] -= labelMat[j]*(Ei - Ej)/eta + alphas[j] = clipAlpha(alphas[j],H,L) + if (abs(alphas[j] - alphaJold) < 0.00001): print("j not moving enough"); continue + alphas[i] += labelMat[j]*labelMat[i]*(alphaJold - alphas[j])#update i by the same amount as j + #the update is in the oppostie direction + b1 = b - Ei- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T + b2 = b - Ej- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T + if (0 < alphas[i]) and (C > alphas[i]): b = b1 + elif (0 < alphas[j]) and (C > alphas[j]): b = b2 + else: b = (b1 + b2)/2.0 + alphaPairsChanged += 1 + print("iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged)) + if (alphaPairsChanged == 0): iter += 1 + else: iter = 0 + print("iteration number: %d" % iter) + return b,alphas \ No newline at end of file