Beispiel #1
0
def testPrc(kTup=('rbf', 10)):
    dataArr, labelArr = loadDataSet('201404')
    print 'load data done'
    b, alphas = smoP(dataArr, labelArr, 200, 0.0001, 10000, kTup)
    datMat = mat(dataArr);
    labelMat = mat(labelArr).transpose()
    svInd = nonzero(alphas.A > 0)[0]
    sVs = datMat[svInd]
    labelSV = labelMat[svInd];
    print "there are %d Support Vectors" % shape(sVs)[0]
    saveSVM(svInd, sVs, labelSV, alphas[svInd])  #保存支持向量
    m, n = shape(datMat)
    errorCount = 0
    for i in range(m):
        kernelEval = kernelTrans(sVs, datMat[i, :], kTup)
        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)
def handwritingIndentify(kTup):
    featureList,labelList = loadImage('digits/trainingDigits')
    print featureList
    print labelList
    b, alphas = svm.smoP(array(featureList),labelList,200,0.0001,10000,kTup)
    print "b:",b, "alpha:",alphas
    svInd = nonzero(alphas.A > 0)[0]
    sVs = mat(array(featureList))[svInd]
    labelSV= mat(labelList).T[svInd]
    print "there a %s Support Vectors."%shape(sVs)[0]
    featureMat = mat(array(featureList));labelMat = mat(labelList).T
    m,n = shape(featureMat)
    errorCount = 0
    for i in range(m):
        kernelMat = svm.kernelTrans(sVs, featureMat[i,:], kTup)
        predict = kernelMat.T * multiply(labelSV,alphas[svInd]) + b
        if sign(predict) != sign(labelMat[i]):
            errorCount += 1
    print "the training data error rate is %s"%(float(errorCount)/m)
Beispiel #3
0
def handwritingIndentify(kTup):
    featureList, labelList = loadImage('digits/trainingDigits')
    print featureList
    print labelList
    b, alphas = svm.smoP(array(featureList), labelList, 200, 0.0001, 10000,
                         kTup)
    print "b:", b, "alpha:", alphas
    svInd = nonzero(alphas.A > 0)[0]
    sVs = mat(array(featureList))[svInd]
    labelSV = mat(labelList).T[svInd]
    print "there a %s Support Vectors." % shape(sVs)[0]
    featureMat = mat(array(featureList))
    labelMat = mat(labelList).T
    m, n = shape(featureMat)
    errorCount = 0
    for i in range(m):
        kernelMat = svm.kernelTrans(sVs, featureMat[i, :], kTup)
        predict = kernelMat.T * multiply(labelSV, alphas[svInd]) + b
        if sign(predict) != sign(labelMat[i]):
            errorCount += 1
    print "the training data error rate is %s" % (float(errorCount) / m)
Beispiel #4
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)
import svm
import numpy as np

dataArr, labelArr = svm.loadDataSet("testSet.txt")
print("dataArr.shape:", np.shape(dataArr), "labelArr.shape:",
      np.shape(labelArr))

#b, alphas = svm.smoSimple(dataArr, labelArr, 0.6, 0.001, 40)

b, alphas = svm.smoP(dataArr, labelArr, 0.6, 0.001, 40)

print("b=", b)
# print("---")
print("alpha.shape:", alphas.shape)

print(alphas[alphas > 0])

# 支持向量
# for i in range(len(dataArr)):
#     if alphas[i] > 0.0:
#         print(dataArr[i], labelArr[i])

w = svm.calcWs(alphas, dataArr, labelArr)
#svm.plot_sv(dataArr, labelArr, w, b, alphas)

i = 5
y_ = svm.predict(dataArr[5], w, b)
print("y_:{}, y:{}".format(y_, labelArr[i]))

svm.eval(dataArr, labelArr, w, b)
Beispiel #6
0
def training(d, l, C = 0.6, tol = 0.01, max_iter = 20, p = True):
    b, alphas = svm.smoP(d, l, C, tol, max_iter)
    if p == True:
        print calcrate(d, l, b, alphas)
    return b, alphas
Beispiel #7
0
loadedlist = dir()
loaded = False
for name in loadedlist:
    if not cmp(name.strip(), u'svm'):
        loaded = True
        print "svm.py loaded"
if (loaded):
    reload(svm)
    print "reload svm"
else:
    import svm
    print "import svm"
dataMat, labelMat = svm.loadDataSet('testSet.txt')
print "loadDataSet()"
#alphas,b = svm.smoSimple(dataMat,labelMat,0.6,0.001,40)
alphas, b = svm.smoP(dataMat, labelMat, 0.6, 0.001, 40)
print "calculate alphas ,bias"
print "plot linegrapha"
svm.plotBestFit(dataMat, labelMat, alphas, b)