# -*- coding: utf-8 -*- # Filename : 02PlattSMO.py from numpy import * import numpy as np import operator import svmMLiA2 import matplotlib.pyplot as plt dataArr, labelArr = svmMLiA2.loadDataSet('nolinear.txt') # print labelArr # 主 platt smo 函数 # 数据集:dataArr # 类别标签:labelArr, # 错分类系数C: 0.6, # 容错率:0.001 # 迭代次数: 40 b, alphas = svmMLiA2.smoP(dataArr, labelArr, 0.6, 0.001, 200) # 根据拉格朗日alphas乘子计算W向量 ws = svmMLiA2.calcWs(alphas, dataArr, labelArr) print("b:",b) print("alphas[alphas > 0]:",alphas[alphas > 0]) # 绘制散点图 mydata = mat(dataArr) # 数据描点 fig = plt.figure() ax = fig.add_subplot(111) for i in range(len(mydata)): if alphas[i]!=0: # KKT条件
# -*- coding: utf-8 -*- # Filename : 03svmRbf.py from numpy import * import numpy as np import operator import svmMLiA2 import matplotlib.pyplot as plt k1 = 1.3 # 加载训练集 dataArr, labelArr = svmMLiA2.loadDataSet('testSetRBF.txt') # 使用Platt SMO分类 # 使用rbf非线性核函数 b, alphas = svmMLiA2.smoP(dataArr, labelArr, 200, 0.0001, 10000, ('rbf', k1)) #C=200 important datMat = mat(dataArr) labelMat = mat(labelArr).T svInd = nonzero(alphas.A > 0)[0] # 获取支持向量 sVs = datMat[svInd] #get matrix of only support vectors labelSV = labelMat[svInd] # 输出支持向量的数量 print "there are %d Support Vectors" % shape(sVs)[0] print svInd m, n = shape(datMat) errorCount = 0 # 计算训练错误率 for i in range(m): kernelEval = svmMLiA2.kernelTrans(sVs, datMat[i, :], ('rbf', k1)) predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b