- Timestamp:
- 09/18/15 16:05:53 (8 months ago)
- Location:
- mystic
- Files:
-
- 2 copied
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/test_svr1.py
r776 r828 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 qld13 13 import pylab 14 14 from mystic.svmtools import * 15 15 16 from mystic.tools import random_seed 17 #random_seed(123) 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) 18 20 21 # linear data with scatter 19 22 x = arange(-5, 5.001) 20 y = x + random.rand(x.size)*7 21 #y = array([9.99, 10.93, 9.11, 6.50, 12.69, 14.09, 13.83, 15.86, 17.49, 14.20, 19.98]) 23 y = x + 7*random.rand(x.size) 22 24 23 25 l = x.size 24 26 N = 2*l 25 26 pylab.plot(x, y, 'k+')27 #pylab.show()28 27 29 28 # the Kernel Matrix (with the linear kernel) … … 33 32 # the linear term for the QP 34 33 Y = concatenate([y,-y]) 35 svr_epsilon = 3 ;34 svr_epsilon = 3 36 35 b = Y + svr_epsilon * ones(Y.size) 37 36 … … 39 38 f = b 40 39 Aeq = concatenate([ones(l), -ones(l)]).reshape(1,N) 41 Beq = array([0 ])40 Beq = array([0.]) 42 41 lb = zeros(N) 43 42 ub = zeros(N) + 0.5 44 43 44 from mystic.symbolic import linear_symbolic, solve, \ 45 generate_solvers as solvers, generate_constraint as constraint 46 constrain = linear_symbolic(Aeq,Beq) 47 constrain = constraint(solvers(solve(constrain,target=['x0']))) 45 48 46 alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 47 #alpha = 2*alpha 49 from mystic import supressed 50 @supressed(1e-5) 51 def conserve(x): 52 return constrain(x) 53 54 from mystic.monitors import VerboseMonitor 55 mon = VerboseMonitor(10) 56 57 from mystic.solvers import diffev 58 alpha = diffev(objective, zip(lb,.1*ub), args=(H,f), npop=N*3, gtol=200, \ 59 itermon=mon, \ 60 ftol=1e-5, bounds=zip(lb,ub), constraints=conserve, disp=1) 61 62 print 'solved x: ', alpha 63 print "constraint A*x == 0: ", inner(Aeq, alpha) 64 print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 48 65 49 66 sv1 = SupportVectors(alpha[:l]) … … 52 69 R = RegressionFunction(x, y, alpha, svr_epsilon, LinearKernel) 53 70 54 pylab.plot(x,R(x)) 55 pylab.plot(x,R(x)+svr_epsilon, 'k--') 56 pylab.plot(x,R(x)-svr_epsilon, 'k--') 71 print 'support vectors: ', sv1, sv2 72 73 # plot data 74 pylab.plot(x, y, 'k+', markersize=10) 75 76 # plot regression function and support 77 pylab.plot(x,R(x), 'k-') 78 pylab.plot(x,R(x)-svr_epsilon, 'r--') 79 pylab.plot(x,R(x)+svr_epsilon, 'g--') 57 80 pylab.plot(x[sv1],y[sv1],'ro') 58 81 pylab.plot(x[sv2],y[sv2],'go') 59 60 print alpha61 62 82 pylab.show() 63 83 64 # $Id$65 #66 84 # end of file -
mystic/examples/test_svr2.py
r776 r828 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: 6 7 # - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE 7 8 """ 8 Support Vector Regression. Example 19 Support Vector Regression. Example 2 9 10 """ 10 11 11 12 from numpy import * 12 import qld13 13 import pylab 14 14 from mystic.svmtools import * 15 15 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) 20 21 # quadratic data plus scatter 16 22 x = arange(-5, 5.001) 17 y = x*x + random.rand(x.size)*8 18 #y = array([9.99, 10.93, 9.11, 6.50, 12.69, 14.09, 13.83, 15.86, 17.49, 14.20, 19.98]) 23 y = x*x + 8*random.rand(x.size) 19 24 20 25 l = x.size 21 26 N = 2*l 22 23 pylab.plot(x, y, 'k+')24 27 25 28 # the Kernel Matrix … … 37 40 f = b 38 41 Aeq = concatenate([ones(l), -ones(l)]).reshape(1,N) 39 Beq = array([0 ])42 Beq = array([0.]) 40 43 lb = zeros(N) 41 44 ub = zeros(N) + 0.5 42 45 43 alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 44 #alpha = 2*alpha 46 from mystic.symbolic import linear_symbolic, solve, \ 47 generate_solvers as solvers, generate_constraint as constraint 48 constrain = linear_symbolic(Aeq,Beq) 49 constrain = constraint(solvers(solve(constrain,target=['x0']))) 50 51 from mystic import supressed 52 @supressed(1e-5) 53 def conserve(x): 54 return constrain(x) 55 56 from mystic.monitors import VerboseMonitor 57 mon = VerboseMonitor(10) 58 59 from mystic.solvers import diffev 60 alpha = diffev(objective, zip(lb,.1*ub), args=(H,f), npop=N*3, gtol=200, \ 61 itermon=mon, \ 62 ftol=1e-5, bounds=zip(lb,ub), constraints=conserve, disp=1) 63 64 print 'solved x: ', alpha 65 print "constraint A*x == 0: ", inner(Aeq, alpha) 66 print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 45 67 46 68 sv1 = SupportVectors(alpha[:l]) … … 49 71 R = RegressionFunction(x, y, alpha, svr_epsilon, pk) 50 72 73 print 'support vectors: ', sv1, sv2 74 75 # plot data 76 pylab.plot(x, y, 'k+', markersize=10) 77 78 # plot regression function and support 51 79 xx = arange(min(x),max(x),0.1) 52 80 pylab.plot(xx,R(xx)) 53 pylab.plot(xx,R(xx) +svr_epsilon, 'k--')54 pylab.plot(xx,R(xx) -svr_epsilon, 'k--')81 pylab.plot(xx,R(xx)-svr_epsilon, 'r--') 82 pylab.plot(xx,R(xx)+svr_epsilon, 'g--') 55 83 pylab.plot(x[sv1],y[sv1],'ro') 56 84 pylab.plot(x[sv2],y[sv2],'go') 57 58 print alpha59 60 85 pylab.show() 61 86 62 # $Id$63 #64 87 # end of file
Note: See TracChangeset
for help on using the changeset viewer.