SVM的代码是实现原理
1)计算核函数
def calcKernelValue(matrix_x, sample_x, kernelOption):
kernelType = kernelOption[0]
numSamples = matrix_x.shape[0]
kernelValue = mat(zeros((numSamples, 1)))
if kernelType == 'linear':
kernelValue = matrix_x * sample_x.T
elif kernelType == 'rbf':
sigma = kernelOption[1]
if sigma == 0:
sigma = 1.0
for i in range(numSamples):
diff = matrix_x[i, :] - sample_x
kernelValue[i] = exp(diff * diff.T / (-2.0 * sigma ** 2))
else:
raise NameError('Not support kernel type! You can use linear or rbf!')
return kernelValue
2)选择最大迭代次数aj
第一步:选择aj
def selectAlpha_j(svm, alpha_i, error_i):
svm.errorCache[alpha_i] =