支持向量机smo算法C语言,支持向量机(SVM),SMO算法原理及源代码剖析

本文深入探讨支持向量机(SVM)及其优化算法SMO,作者整理了网上资料,完整推导了SMO算法的数学过程,并提供了一个简单的Python实现。该实现未涉及核函数,仅使用内积,旨在清晰解释SVM的工作原理。同时,文章通过截图形式呈现无法直接在论坛展示的公式。
摘要由CSDN通过智能技术生成

参考了网上一些其他人写的博客,都有或多或少的缺陷,比如CSDN上将SVM浏览量最多的那个,他讲到SMO算法中,alpha和b的更新值他直接给出了结果,没有推导。

我总结了网上别人的东西,将SVM,SMO中所有的数学推理从头到尾整理了出来。

最后代码中没有kernal的转换,只是内积,主要是想给大家把SVM的原理讲明白。最后的Python源代码(来自Machine Learning in Action)只是一个简单的smo算法,帮助大家理解SVM,详细的源代码请大家去下libSVM。

文章在word里写好了,公式没法贴到论坛里,所以直接截图了

http___img0.tuicool.com_N3au22.jpg

http___img1.tuicool.com_n2EjIf.jpg

http___img2.tuicool.com_7F363e.jpg

http___img0.tuicool.com_MzQjam.jpg

http___img1.tuicool.com_U77J3yy.jpg

http___img2.tuicool.com_VB7BJn.jpg

http___img0.tuicool.com_JJ3e6f.jpg

http___img1.tuicool.com_qa6nQ3.jpg

http___img2.tuicool.com_3aiqme.jpg

http___img0.tuicool.com_jEvQja.jpg

http___img1.tuicool.com_6vYRJju.jpg

下面是Python写到简单smo算法

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)))#alphas和b的初值都为0

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)):#不满足KKT条件就更新

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值