标签:math 影响 lan == svm 也有 gif sts spl
师兄博客原文地址
到平面的距离为为:
则通过拉格朗日乘子法得到的拉格朗日函数如下:
令\(L(\bm{w},b,\bm{\alpha},\bm{\xi},\bm{\mu})\)对\(\bm{w},b,\xi_i\)求偏导可得:
将(2)式代入(1)式,有原问题的对偶问题:
对应的KKT条件要求:
最小二乘支持向量机使用均方误差作为软间隔支持向量机中的替代损失,并修改约束有式:
拉格朗日对偶为:
令\(L(\bm{w},b,\bm{\alpha},\bm{e})\)对\(\bm{w},b,e_i\)求偏导可得:
通过计算可直接求得\(\bm{\alpha}\)和\(b\):
其中:
def trainLSSVM(kMat, trainY, gamma):
omiga = multiply(trainY@trainY.T, kMat)
rows = omiga.shape[0]
vecOne = ones((rows, 1))
H = omiga + eye(rows)/gamma
b = trainY.T@(linalg.inv(H)@vecOne)/(trainY.T@(linalg.inv(H)@vecOne))
alpha = linalg.inv(H)@(vecOne - trainY@b)
return alpha, b
def kernelmat(sigma, trainX, testX):
trainRows = trainX.shape[0]
if testX is None:
trainX2 = sum(trainX**2, 0)@ones((1, trainRows))
kMat = trainX2 + trainX2.T - 2*(trainX*trainX.T)
else:
testRows = testX.shape[0]
trainX2 = sum(trainX**2, 0)@ones((1, testRows))
testX2 = sum(testX**2, 0)@ones((1, trainRows))
kMat = trainX2 + testX2.T - 2*(trainX*testX.T)
kMat = -kMat/(sigma**2*2)
kMat = exp(kMat)
return kMat
def simLSSVM(trainX, trainY, testX, alpha, b, sigma):
kMat = kernelmat(sigma, trainX, testX)
testY = kMat.T@(multiply(alpha, trainY))
testY = sum(testY, 0) + b
testY = sign(testY)
return testY
LSSVM模型在\(\gamma\)和\(\sigma\)确定的情况下是可以通过计算求出解析解的。然而因为有替代损失和核函数的存在,通常LSSVM模型需要优化两个参数。
这里以交叉验证的错误率作为目标函数值,使用模拟退火法对其进行优化
def crossvalidateCost(trainX, trainY, kMat, gamma, sigma, L):
rows = trainY.shape[0]
blockSize = rows/L
costs = zeros((L, 1))
vecIndex = range(0, rows)
vecIndex = mat(vecIndex)
i = 1
for x in costs:
if i == 0:
validation = blockSize*(i-1) + 1 + vecIndex
else:
validation = blockSize * (i - 1) + 1 + mat(range(0, blockSize))
training = setdiff1d(vecIndex, validation)
subK = kMat[training, training]
subX = trainX[training, :]
subY = trainY[training, :]
testX = trainX[validation, :]
trueY = trainY[validation, :]
[alpha, b] = trainLSSVM(subK, subY, gamma)
testY = simLSSVM(subX, subY, testX, alpha, b, sigma)
x = 1 - (sum(testY == trueY)/len(testY))
return mean(costs)
# 优化gamma和sigma
gammaOrg = 10
sigmaOrg = 0.45
[gamma, sigma] = SAA(trainX, trainY, gammaOrg, sigmaOrg)
# 训练模型
kMat = kernelmat(sigma, trainX, testX)
[alpha, b] = trainLSSVM(kMat, trainY, gamma)
# 分类
testY = simLSSVM(trainX, trainY, testX, alpha, b, sigma)
acc = sum(testY == trueY)/len(trueY)
标签:math 影响 lan == svm 也有 gif sts spl
原文地址:https://www.cnblogs.com/2944014083-zhiyu/p/14871851.html