项目场景:
alpha的选取,随机选择一个不等于i值的j
代码:
def selectJrand(i,m):
j=i
while (j==i):
j = int(random.uniform(0,m))
return j
</font>
# 进行剪辑:
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
b = 0;
#获取数据维度
m,n = shape(dataMatrix)
#初始化所有alpha为0
alphas = mat(zeros((m,1)))
iter = 0
#迭代求解
while (iter < maxIter):
alphaPairsChanged = 0
for i in range(m):
#计算g(xi)
gXi = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b
#计算Ei
Ei = gXi