【机器学习算法-python实现】svm支持向量机(2)—简化版SMO算法

(转载请注明出处:http://blog.csdn.net/buptgshengod)

1.背景知识

     通过上一节我们通过引入拉格朗日乗子得到支持向量机变形公式。详细变法可以参考这位大神的博客——地址
  参照拉格朗日公式F(x1,x2,...λ)=f(x1,x2,...)-λg(x1,x2...)。我们把上面的式子变型为:

 约束条件就变成了:
下面就根据最小优化算法SMO(Sequential Minimal Optimization)。找出距离分隔面最近的点,也就是支持向量集。如下图的蓝色点所示。





2.代码

import matplotlib.pyplot as plt
from numpy import *
from time import sleep

def loadDataSet(fileName):
    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):
    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):
    if aj > H: 
        aj = H
    if L > aj:
        aj = L
    return aj

def smoSimple(dataMatIn, classLabels, C, toler, maxIter):
    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

def matplot(dataMat,lableMat):
    xcord1 = []; ycord1 = []
    xcord2 = []; ycord2 = []
    xcord3 = []; ycord3 = []
    for i in range(100):
        if lableMat[i]==1:
           xcord1.append(dataMat[i][0])
           ycord1.append(dataMat[i][1])
        else:
           xcord2.append(dataMat[i][0])
           ycord2.append(dataMat[i][1]) 
    b,alphas=smoSimple(dataMat,labelMat,0.6,0.001,40)
    for j in range(100):
        if alphas[j]>0:
             xcord3.append(dataMat[j][0])
             ycord3.append(dataMat[j][1])               
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='green')
    ax.scatter(xcord3, ycord3, s=80, c='blue')
    ax.plot()
    plt.xlabel('X1'); plt.ylabel('X2');
    plt.show()    

if __name__=='__main__':
     dataMat,labelMat=loadDataSet('/Users/hakuri/Desktop/testSet.txt')
#      b,alphas=smoSimple(dataMat,labelMat,0.6,0.001,40)  
#      print b,alphas[alphas>0]
     matplot(dataMat,labelMat)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Python支持向量机(Support Vector Machine,SVM)是一种非常常用的机器学习算法,用于分类和回归问题。下面是关于其设计与实现的简要说明: 首先,我们需要导入所需的库,例如`numpy`用于数值计算,`sklearn.svm`用于实现SVM模型。然后,我们可以通过调用`svm.SVC()`创建一个支持向量机分类模型。 接下来,我们需要准备训练集和测试集的数据。通常,我们需要将数据集分为输入特征(X)和目标变量(y)。特征是用于训练模型的属性,而目标变量是我们希望预测的输出。确保数据已经适当地进行了预处理,例如特征缩放。 然后,我们可以使用`fit(X, y)`方法拟合我们的模型,这将根据训练集的特征和目标变量训练模型。之后,我们可以使用`predict(X_test)`方法对测试集的特征进行预测,并得到预测结果。 在实际实施中,我们还可以调整一些参数来优化模型的性能。例如,我们可以调整正则化参数C值,以控制模型对误分类样本的惩罚程度;还可以选择不同的核函数,例如线性核、多项式核或高斯径向基函数(RBF)核,以适应不同的数据分布。 最后,我们可以使用准确率、精确率、召回率等指标来评估我们的模型的性能。这些指标可以通过引入`sklearn.metrics`库实现。 总之,基于PythonSVM算法实现涉及导入相关库、创建模型、准备数据、训练模型、预测及评估模型。这种机器学习算法非常适用于分类和回归问题,尤其对于非线性数据或具有高维特征的数据集效果显著。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值