def testRbf(k1=1.3): dataArr, labelArr = svm.loadDataSet('testSetRBF.txt') b, alphas = PlattSMO(dataArr, labelArr, 200, 0.0001, 10000, ('rbf', k1)) # C=200 important datMat = mat(dataArr) labelMat = mat(labelArr).transpose() svInd = nonzero(alphas.A > 0)[0] # 取非零alphas值的索引,构建支持向量的矩阵 sVs = datMat[svInd] # get matrix of only support vectors labelSV = labelMat[svInd] print "there are %d Support Vectors" % shape(sVs)[0] m, n = shape(datMat) errorCount = 0 ''' w = Sigma(alpha_i * y_i * phi(x_i)),<Sigma为求和> g_svm(x)=sign(w.T*phi(x)+b)=sign(Sigma(alpha_i*y_i*phi(x_i)*phi(x))+b)=sign(Sigma(alpha_i*y_i*kernel(x_i,x))+b) <kernel()为核函数,phi()为高维空间的映射函数,alpha_i>0,对应的x_i为支持向量> ''' for i in range(m): kernelEval = kernelTrans(sVs, datMat[i, :], ('rbf', k1)) predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b if sign(predict) != sign(labelArr[i]): errorCount += 1 print "the training error rate is: %f" % (float(errorCount) / m) dataArr, labelArr = svm.loadDataSet('testSetRBF2.txt') errorCount = 0 datMat = mat(dataArr) labelMat = mat(labelArr).transpose() m, n = shape(datMat) for i in range(m): kernelEval = kernelTrans(sVs, datMat[i, :], ('rbf', k1)) predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b if sign(predict) != sign(labelArr[i]): errorCount += 1 print "the test error rate is: %f" % (float(errorCount) / m)
import svm from numpy import * dataMat,labelMat=svm.loadDataSet('testSet.txt') #print(labelMat) b,alphas=svm.smoSimple(dataMat,labelMat,0.6,0.001,40) print(b)
#coding:utf-8 import svm dataArr, labelArr = svm.loadDataSet('testSet.txt') #print labelArr b, alphas = svm.smoSimple(dataArr, labelArr, 0.6, 0.001, 40) print b print alphas[alphas > 0]
# -*- coding: utf-8 -*- """ Created on Wed Sep 5 15:22:26 2018 @author: weixw """ import svm as sm #通过训练数据计算 b, alphas dataArr, labelArr = sm.loadDataSet('train.txt') b, alphas = sm.smoP(dataArr, labelArr, 200, 0.0001, 10000, ('rbf', 0.10)) sm.drawDataMap(dataArr, labelArr, b, alphas) sm.getTrainingDataResult(dataArr, labelArr, b, alphas, 0.10) dataArr1, labelArr1 = sm.loadDataSet('test.txt') #测试结果 sm.getTestDataResult(dataArr1, labelArr1, b, alphas, 0.10)
#!usr/bin/env python3 # -*- coding:utf-8 -*- """ #@author:Benny.Chen #@file: main.py #@time: 2020/6/5 11:50 #@email:[email protected] """ import svm if __name__ == '__main__': dataArr,labelArr = svm.loadDataSet("testSet.txt") b,alphas = svm.smoSimple(dataArr, labelArr, 0.6, 0.001, 40)
import svm from numpy import * dataArr, labelArr = svm.loadDataSet("machinelearninginaction/Ch06/testSet.txt") print(labelArr) print(dataArr)
def testRbf(k1=1.3): dataArr, labelArr = svm.loadDataSet('dataset.txt') alphas, b = smoP(dataArr, labelArr, 200, 0.0001, 10000, ('rbf', k1))