Example #1
0
def dummy():
    ## a very simple test
    xDummy = numpy.array([[0, 0], [1, 1], [1, 2]])
    assert xDummy.shape == (3, 2)
    ## quadratic basis function
    M = 2
    phiDummy = makePhi(xDummy, M)
    assert phiDummy.shape == (3, 6)
    n, m = phiDummy.shape
    yDummy = numpy.array([[-1], [1], [1]])
    assert yDummy.shape == (3, 1)

    # Carry out training, primal and/or dual
    C = 10e10
    C = 0.25
    p = svm.primal(phiDummy, yDummy, C)
    w = numpy.array(p['x'][:m])
    assert w.shape == (6, 1)
    b = p['x'][m]
    assert isinstance(b, (int, float))
    dummyPredictor = makePredictor(w, b, M, 'svm')

    plotDecisionBoundary(xDummy,
                         yDummy,
                         dummyPredictor, [-1, 0, 1],
                         title='SVM Validate')
Example #2
0
 def _train(self, X, Y):
     ## save for use in _getPredictor below
     self.tX = X
     self.tY = Y
     ## first, cvxopt output suppression
     if not self.printInfo: cvxopt.solvers.options['show_progress'] = False
     phi = makePhi(X,self.M)
     n,m = phi.shape
     primal = self.params['primal']
     C = self.params['C']
     if primal:
         self.result = svm.primal(phi,Y,C)
         w = numpy.array(self.result['x'][:m])
         b = self.result['x'][m]
     else:
         self.kernel = self.params['kernel']
         self.result = svm.dual(phi,Y,C,self.kernel)
         self.alphaD = numpy.array(self.result['x'])
         w,b,self.dualS,self.dualM = svm.dualWeights(phi, Y, self.kernel, self.alphaD, C)
         self.sv = self.numSupport(self.alphaD)
     self.slack = numpy.array(self.result['z'])[:-n]
     return w,b
Example #3
0
def dummy():
    ## a very simple test
    xDummy = numpy.array([[0,0],[1,1],[1,2]])
    assert xDummy.shape == (3,2)
    ## quadratic basis function
    M=2
    phiDummy = makePhi(xDummy,M)
    assert phiDummy.shape == (3,6)
    n,m = phiDummy.shape
    yDummy = numpy.array([[-1], [1], [1]])
    assert yDummy.shape == (3,1)

    # Carry out training, primal and/or dual
    C = 10e10
    C = 0.25
    p = svm.primal(phiDummy,yDummy,C)
    w = numpy.array(p['x'][:m])
    assert w.shape == (6,1)
    b = p['x'][m]
    assert isinstance(b, (int,float))
    dummyPredictor = makePredictor(w,b,M,'svm')

    plotDecisionBoundary(xDummy, yDummy, dummyPredictor, [-1, 0, 1], title = 'SVM Validate')
Example #4
0
 def _train(self, X, Y):
     ## save for use in _getPredictor below
     self.tX = X
     self.tY = Y
     ## first, cvxopt output suppression
     if not self.printInfo: cvxopt.solvers.options['show_progress'] = False
     phi = makePhi(X, self.M)
     n, m = phi.shape
     primal = self.params['primal']
     C = self.params['C']
     if primal:
         self.result = svm.primal(phi, Y, C)
         w = numpy.array(self.result['x'][:m])
         b = self.result['x'][m]
     else:
         self.kernel = self.params['kernel']
         self.result = svm.dual(phi, Y, C, self.kernel)
         self.alphaD = numpy.array(self.result['x'])
         w, b, self.dualS, self.dualM = svm.dualWeights(
             phi, Y, self.kernel, self.alphaD, C)
         self.sv = self.numSupport(self.alphaD)
     self.slack = numpy.array(self.result['z'])[:-n]
     return w, b