针对二分类问题的支持向量机模型
以下为基于SMO算法的SVM求解代码:
# 训练SVM模型
import matplotlib.pyplot as plt
import numpy as np
DataSet = [[3.0,3.0,1.0],
[3.0,15.0,1.0],
[4.0,3.0,1.0],
[2.0,10.0,1.0],
[1.0,1.0,-1.0],
[0,2.0,-1.0],
[-1.0,1.0,-1.0]]
NumData = len(DataSet)
NumAttr = len(DataSet[0])-1
# 初始化时也必须满足约束条件
while 1:
A = np.random.rand(NumData)
A[NumData-1] = sum([-A[m]*DataSet[m][-1] for m in range(NumData-1)])/DataSet[NumData-1][-1]
if min(A)>=0:
break
W = np.random.randn(NumAttr)
b = np.random.randn(1)
MaxTimes = 1000
TimeCal = 0
C = 3e8
while TimeCal
H:
A2new=H
if A2new
0)and(A[i]
0)and(A[j]