- Timestamp:
- 09/18/15 09:51:13 (8 months ago)
- Location:
- mystic/examples
- Files:
-
- 2 copied
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/test_svc1.py
r826 r827 2 2 # 3 3 # Author: Patrick Hung (patrickh @caltech) 4 # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 4 5 # Copyright (c) 1997-2015 California Institute of Technology. 5 6 # License: 3-clause BSD. The full license text is available at: … … 10 11 11 12 from numpy import * 12 import qld, quapro13 13 import pylab 14 14 from mystic.svctools import * 15 15 16 def myshow(): 17 import Image, tempfile 18 name = tempfile.mktemp() 19 pylab.savefig(name,dpi=72) 20 im = Image.open('%s.png' % name) 21 im.show() 16 # a common objective function for solving a QP problem 17 # (see http://www.mathworks.com/help/optim/ug/quadprog.html) 18 def objective(x, H, f): 19 return 0.5 * dot(dot(x,H),x) + dot(f,x) 22 20 23 21 c1 = array([[0., 0.],[1., 0.],[ 0.2, 0.2],[0.,1.]]) 24 22 c2 = array([[0, 1.1], [1.1, 0.],[0, 1.5],[0.5,1.2],[0.8, 1.7]]) 25 23 26 pylab.plot(c1[:,0], c1[:,1], 'ko')27 pylab.plot(c2[:,0], c2[:,1], 'ro')28 29 24 # the Kernel Matrix (with the linear kernel) 30 # Q = multiply.outer(X,X) <--- unfortunately only works when X is a list of scalars... 31 # In Mathematica, this would be implemented simply via Outer[K, X, X, 1] 25 # Q = multiply.outer(X,X) # NOTE: requires X is a list of scalars 32 26 33 27 XX = concatenate([c1,-c2]) … … 41 35 f = b 42 36 Aeq = concatenate([ones(c1.shape[0]), -ones(c2.shape[0])]).reshape(1,nx) 43 Beq = array([0 ])37 Beq = array([0.]) 44 38 lb = zeros(nx) 45 ub = zeros(nx) + 9999939 ub = 99999 * ones(nx) 46 40 47 #alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 48 alpha, xxx= quapro.quadprog(H, f, None, None, Aeq=Aeq, beq=Beq, LB=lb, UB=ub) 49 print alpha 50 #alpha = array([fil(x) for x in alpha]) 51 print "cons:", inner(Aeq,alpha) 52 print "obj min: 0.5 * x'Hx + <x,f>", 0.5*inner(alpha,inner(H,alpha))+inner(f,alpha) 41 from mystic.symbolic import linear_symbolic, solve, \ 42 generate_solvers as solvers, generate_constraint as constraint 43 constrain = linear_symbolic(Aeq,Beq) 44 constrain = constraint(solvers(solve(constrain,target=['x0']))) 53 45 46 from mystic import supressed 47 @supressed(1e-5) 48 def conserve(x): 49 return constrain(x) 50 51 #from mystic.monitors import VerboseMonitor 52 #mon = VerboseMonitor(1) 53 54 from mystic.solvers import diffev 55 alpha = diffev(objective, zip(lb,ub), args=(H,f), npop=nx*3, gtol=200, \ 56 # itermon=mon, \ 57 ftol=1e-8, bounds=zip(lb,ub), constraints=conserve, disp=1) 58 59 print 'solved x: ', alpha 60 print "constraint A*x == 0: ", inner(Aeq, alpha) 61 print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 54 62 55 63 # the labels and the points … … 66 74 bias2 = -0.5 *( max(ii[ym]) + min(ii[yp]) ) 67 75 68 print wv69 print sv1, sv270 print bias71 print bias276 print 'weight vector: ', wv 77 print 'support vectors: ', sv1, sv2 78 print 'bias (from points): ', bias 79 print 'bias (with vectors): ', bias2 72 80 73 # Eqn of hyperplane: 74 # wv[0] x + wv[1] y + bias = 0 75 hx = array([0, 1.2]) 81 # plot data 82 pylab.plot(c1[:,0], c1[:,1], 'bo') 83 pylab.plot(c2[:,0], c2[:,1], 'yo') 84 85 # plot hyperplane: wv[0] x + wv[1] y + bias = 0 86 xmin,xmax,ymin,ymax = pylab.axis() 87 hx = array([floor(xmin-.1), ceil(xmax+.1)]) 76 88 hy = -wv[0]/wv[1] * hx - bias/wv[1] 89 pylab.plot(hx, hy, 'k-') 90 #pylab.axis([xmin,xmax,ymin,ymax]) 91 pylab.show() 77 92 78 pylab.plot(hx, hy, 'k-')79 myshow()80 81 # $Id$82 #83 93 # end of file -
mystic/examples/test_svc2.py
r826 r827 2 2 # 3 3 # Author: Patrick Hung (patrickh @caltech) 4 # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 4 5 # Copyright (c) 1997-2015 California Institute of Technology. 5 6 # License: 3-clause BSD. The full license text is available at: … … 7 8 """ 8 9 Support Vector Classification. Example 2. 9 meristem data. 10 11 using meristem data from data files 10 12 """ 11 13 12 14 from numpy import * 13 from scipy.io import read_array14 import qld, quapro15 15 import pylab 16 16 from mystic.svctools import * 17 import os.path , time17 import os.path 18 18 19 def myshow(): 20 import Image, tempfile 21 name = tempfile.mktemp() 22 pylab.savefig(name,dpi=150) 23 im = Image.open('%s.png' % name) 24 im.show() 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) 25 23 26 c1 = read_array(os.path.join('DATA','g1x.pts')) 27 c2 = read_array(os.path.join('DATA','g2.pts')) 28 c1[:,0] += 5 # to make the two sets overlap a little 24 # SETTINGS 25 reduced = True # use a subset of the full data 26 overlap = False # reduce the distance between the datasets 29 27 30 # interchange ? 31 #c1, c2 = c2, c1 28 c1 = loadtxt(os.path.join('DATA','g1.pts')) 29 c2 = loadtxt(os.path.join('DATA','g2.pts')) 30 31 if reduced: 32 c1 = c1[c1[:,0] > 245] 33 c2 = c2[c2[:,0] < 280] 34 if overlap: c1[:,0] += 3 # make the datasets overlap a little 32 35 33 36 # the Kernel Matrix (with the linear kernel) 34 # Q = multiply.outer(X,X) <--- unfortunately only works when X is a list of scalars... 35 # In Mathematica, this would be implemented simply via Outer[K, X, X, 1] 37 # Q = multiply.outer(X,X) # NOTE: requires X is a list of scalars 36 38 37 39 XX = concatenate([c1,-c2]) … … 45 47 f = b 46 48 Aeq = concatenate([ones(c1.shape[0]), -ones(c2.shape[0])]).reshape(1,nx) 47 Beq = array([0 ])49 Beq = array([0.]) 48 50 lb = zeros(nx) 49 ub = zeros(nx) + 100000 51 ub = 1 * ones(nx) 52 _b = .1 * ones(nx) # good starting value if most solved xi should be zero 50 53 51 #alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 52 alpha,xxx = quapro.quadprog(H, f, None, None, Aeq, Beq, lb, ub) 53 #t1 = time.time() 54 #for i in range(1000): 55 # alpha,xxx = quapro.quadprog(H, f, None, None, Aeq, Beq, lb, ub) 56 # #alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 57 #t2 = time.time() 58 #print "1000 calls to QP took %0.3f s" % (t2-t1) 59 print alpha 54 from mystic.symbolic import linear_symbolic, solve, \ 55 generate_solvers as solvers, generate_constraint as constraint 56 constrain = linear_symbolic(Aeq,Beq) 57 #NOTE: assumes a single equation of the form: '1.0*x0 + ... = 0.0\n' 58 x0,rhs = constrain.strip().split(' = ') 59 x0,xN = x0.split(' + ', 1) 60 N,x0 = x0.split("*") 61 constrain = "{x0} = ({rhs} - ({xN}))/{N}".format(x0=x0, xN=xN, N=N, rhs=rhs) 62 #NOTE: end slight hack (as mystic.symbolic.solve takes __forever__) 63 constrain = constraint(solvers(constrain)) 64 65 from mystic import supressed 66 @supressed(5e-2) 67 def conserve(x): 68 return constrain(x) 69 70 from mystic.monitors import VerboseMonitor 71 mon = VerboseMonitor(10) 72 73 from mystic.solvers import diffev 74 alpha = diffev(objective, zip(lb,_b), args=(H,f), npop=nx*3, gtol=200,\ 75 itermon=mon, \ 76 ftol=1e-8, bounds=zip(lb,ub), constraints=conserve, disp=1) 77 78 print 'solved x: ', alpha 79 print "constraint A*x == 0: ", inner(Aeq, alpha) 80 print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 60 81 61 82 # the labels and the points … … 64 85 65 86 wv = WeightVector(alpha, X, y) 66 sv1, sv2 = SupportVectors(alpha,y, 87 sv1, sv2 = SupportVectors(alpha,y,eps=1e-6) 67 88 bias = Bias(alpha, X, y) 68 89 69 print wv 70 print sv1, sv2 71 print bias 90 ym = (y.flatten()<0).nonzero()[0] 91 yp = (y.flatten()>0).nonzero()[0] 92 ii = inner(wv, X) 93 bias2 = -0.5 *( max(ii[ym]) + min(ii[yp]) ) 72 94 73 # Eqn of hyperplane: 74 # wv[0] x + wv[1] y + bias = 0 95 print 'weight vector: ', wv 96 print 'support vectors: ', sv1, sv2 97 print 'bias (from points): ', bias 98 print 'bias (with vectors): ', bias2 75 99 76 pylab.plot(c1[:,0], c1[:,1], 'ko',markersize=2) 77 pylab.plot(c2[:,0], c2[:,1], 'ro',markersize=2) 100 # plot data 101 pylab.plot(c1[:,0], c1[:,1], 'bo', markersize=5) 102 pylab.plot(c2[:,0], c2[:,1], 'yo', markersize=5) 103 104 # plot hyperplane: wv[0] x + wv[1] y + bias = 0 78 105 xmin,xmax,ymin,ymax = pylab.axis() 79 hx = array([ xmin, xmax])106 hx = array([floor(xmin-.1), ceil(xmax+.1)]) 80 107 hy = -wv[0]/wv[1] * hx - bias/wv[1] 81 108 pylab.plot(hx, hy, 'k-') 82 pylab.axis([xmin,xmax,ymin,ymax]) 83 pylab.plot(XX[sv1,0], XX[sv1,1], 'ko',markersize=5) 84 pylab.plot(-XX[sv2,0], -XX[sv2,1], 'ro',markersize=5) 85 pylab.axis('equal') 86 myshow() 109 #pylab.axis([xmin,xmax,ymin,ymax]) 87 110 88 # $Id$ 89 # 111 # 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) 114 #pylab.axis('equal') 115 pylab.show() 116 90 117 # end of file
Note: See TracChangeset
for help on using the changeset viewer.