Changeset 830 for mystic/examples/test_svc2.py
- Timestamp:
- 09/19/15 11:26:58 (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/test_svc2.py
r827 r830 17 17 import os.path 18 18 19 # a common objective function for solving a QP problem 20 # (see http://www.mathworks.com/help/optim/ug/quadprog.html) 21 def objective(x, H, f): 22 return 0.5 * dot(dot(x,H),x) + dot(f,x) 19 # define the objective function as the dual for SVC 20 # (see: http://scikit-learn.org/stable/modules/svm.html#svc) 21 # the objective funciton is very similar to the dual for SVC 22 # (see: http://scikit-learn.org/stable/modules/svm.html#svc) 23 def objective(x, Q, b): 24 return 0.5 * dot(dot(x,Q),x) + dot(b,x) 23 25 24 26 # SETTINGS … … 26 28 overlap = False # reduce the distance between the datasets 27 29 30 # define the data points for each class 28 31 c1 = loadtxt(os.path.join('DATA','g1.pts')) 29 32 c2 = loadtxt(os.path.join('DATA','g2.pts')) … … 34 37 if overlap: c1[:,0] += 3 # make the datasets overlap a little 35 38 36 # the Kernel Matrix (with the linear kernel) 37 # Q = multiply.outer(X,X) # NOTE: requires X is a list of scalars 39 # define the full data set 40 X = concatenate([c1,c2]); nx = X.shape[0] 41 # define the labels (+1 for c1; -1 for c2) 42 y = concatenate([ones(c1.shape[0]), -ones(c2.shape[0])]).reshape(1,nx) 38 43 44 # build the Kernel Matrix (with the linear kernel) 45 # get the QP quadratic and linear terms 39 46 XX = concatenate([c1,-c2]) 40 nx = XX.shape[0] 47 Q = KernelMatrix(XX) # Q_ij = K(x_i, x_j) 48 b = -ones(nx) # b_i = 1 (in dual) 41 49 42 # quadratic and linear terms of QP 43 Q = KernelMatrix(XX) 44 b = -1 * ones(nx) 45 46 H = Q 47 f = b 48 Aeq = concatenate([ones(c1.shape[0]), -ones(c2.shape[0])]).reshape(1,nx) 50 # build the constraints (y.T * x = 0.0) 51 # 1.0*x0 + 1.0*x1 + ... - 1.0*xN = 0.0 52 Aeq = y 49 53 Beq = array([0.]) 54 # set the bounds 50 55 lb = zeros(nx) 51 56 ub = 1 * ones(nx) 52 57 _b = .1 * ones(nx) # good starting value if most solved xi should be zero 53 58 59 # build the constraints operator 54 60 from mystic.symbolic import linear_symbolic, solve, \ 55 61 generate_solvers as solvers, generate_constraint as constraint … … 71 77 mon = VerboseMonitor(10) 72 78 79 # solve the dual for alpha 73 80 from mystic.solvers import diffev 74 alpha = diffev(objective, zip(lb,_b), args=( H,f), npop=nx*3, gtol=200,\81 alpha = diffev(objective, zip(lb,_b), args=(Q,b), npop=nx*3, gtol=200,\ 75 82 itermon=mon, \ 76 83 ftol=1e-8, bounds=zip(lb,ub), constraints=conserve, disp=1) … … 78 85 print 'solved x: ', alpha 79 86 print "constraint A*x == 0: ", inner(Aeq, alpha) 80 print "minimum 0.5*x' Hx + f'*x: ", objective(alpha, H, f)87 print "minimum 0.5*x'Qx + b'*x: ", objective(alpha, Q, b) 81 88 82 # the labels and the points 83 X = concatenate([c1,c2]) 84 y = concatenate([ones(c1.shape[0]), -ones(c2.shape[0])]).reshape(1,nx) 85 89 # calculate weight vectors, support vectors, and bias 86 90 wv = WeightVector(alpha, X, y) 87 91 sv1, sv2 = SupportVectors(alpha,y,eps=1e-6) … … 110 114 111 115 # plot the support points 112 pylab.plot(XX[sv1,0], XX[sv1,1], 'b ^',markersize=10)113 pylab.plot(-XX[sv2,0], -XX[sv2,1], 'y ^',markersize=10)116 pylab.plot(XX[sv1,0], XX[sv1,1], 'bo', markersize=8) 117 pylab.plot(-XX[sv2,0], -XX[sv2,1], 'yo', markersize=8) 114 118 #pylab.axis('equal') 115 119 pylab.show()
Note: See TracChangeset
for help on using the changeset viewer.