Changeset 828


Ignore:
Timestamp:
09/18/15 16:05:53 (8 months ago)
Author:
mmckerns
Message:

update SV regression examples from examples_other

Location:
mystic
Files:
2 copied
2 moved

Legend:

Unmodified
Added
Removed
  • mystic/examples/test_svr1.py

    r776 r828  
    22# 
    33# Author: Patrick Hung (patrickh @caltech) 
     4# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 
    45# Copyright (c) 1997-2015 California Institute of Technology. 
    56# License: 3-clause BSD.  The full license text is available at: 
     
    1011 
    1112from numpy import * 
    12 import qld 
    1313import pylab 
    1414from mystic.svmtools import * 
    1515 
    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) 
     18def objective(x, H, f): 
     19    return 0.5 * dot(dot(x,H),x) + dot(f,x) 
    1820 
     21# linear data with scatter 
    1922x = 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]) 
     23y = x + 7*random.rand(x.size) 
    2224 
    2325l = x.size 
    2426N = 2*l 
    25  
    26 pylab.plot(x, y, 'k+') 
    27 #pylab.show() 
    2827 
    2928# the Kernel Matrix (with the linear kernel) 
     
    3332# the linear term for the QP 
    3433Y = concatenate([y,-y]) 
    35 svr_epsilon = 3; 
     34svr_epsilon = 3 
    3635b = Y + svr_epsilon * ones(Y.size) 
    3736 
     
    3938f = b 
    4039Aeq = concatenate([ones(l), -ones(l)]).reshape(1,N) 
    41 Beq = array([0]) 
     40Beq = array([0.]) 
    4241lb = zeros(N) 
    4342ub = zeros(N) + 0.5 
    4443 
     44from mystic.symbolic import linear_symbolic, solve, \ 
     45     generate_solvers as solvers, generate_constraint as constraint 
     46constrain = linear_symbolic(Aeq,Beq) 
     47constrain = constraint(solvers(solve(constrain,target=['x0']))) 
    4548 
    46 alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 
    47 #alpha = 2*alpha 
     49from mystic import supressed 
     50@supressed(1e-5) 
     51def conserve(x): 
     52    return constrain(x) 
     53 
     54from mystic.monitors import VerboseMonitor 
     55mon = VerboseMonitor(10) 
     56 
     57from mystic.solvers import diffev 
     58alpha = 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 
     62print 'solved x: ', alpha 
     63print "constraint A*x == 0: ", inner(Aeq, alpha) 
     64print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 
    4865 
    4966sv1 = SupportVectors(alpha[:l]) 
     
    5269R = RegressionFunction(x, y, alpha, svr_epsilon, LinearKernel) 
    5370 
    54 pylab.plot(x,R(x)) 
    55 pylab.plot(x,R(x)+svr_epsilon, 'k--') 
    56 pylab.plot(x,R(x)-svr_epsilon, 'k--') 
     71print 'support vectors: ', sv1, sv2 
     72 
     73# plot data 
     74pylab.plot(x, y, 'k+', markersize=10) 
     75 
     76# plot regression function and support 
     77pylab.plot(x,R(x), 'k-') 
     78pylab.plot(x,R(x)-svr_epsilon, 'r--') 
     79pylab.plot(x,R(x)+svr_epsilon, 'g--') 
    5780pylab.plot(x[sv1],y[sv1],'ro') 
    5881pylab.plot(x[sv2],y[sv2],'go') 
    59  
    60 print alpha 
    61  
    6282pylab.show() 
    6383 
    64 # $Id$ 
    65 #  
    6684# end of file 
  • mystic/examples/test_svr2.py

    r776 r828  
    22# 
    33# Author: Patrick Hung (patrickh @caltech) 
     4# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) 
    45# Copyright (c) 1997-2015 California Institute of Technology. 
    56# License: 3-clause BSD.  The full license text is available at: 
    67#  - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE 
    78""" 
    8 Support Vector Regression. Example 1 
     9Support Vector Regression. Example 2 
    910""" 
    1011 
    1112from numpy import * 
    12 import qld 
    1313import pylab 
    1414from mystic.svmtools import * 
    1515 
     16# a common objective function for solving a QP problem 
     17# (see http://www.mathworks.com/help/optim/ug/quadprog.html) 
     18def objective(x, H, f): 
     19    return 0.5 * dot(dot(x,H),x) + dot(f,x) 
     20 
     21# quadratic data plus scatter 
    1622x = 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]) 
     23y = x*x + 8*random.rand(x.size) 
    1924 
    2025l = x.size 
    2126N = 2*l 
    22  
    23 pylab.plot(x, y, 'k+') 
    2427 
    2528# the Kernel Matrix  
     
    3740f = b 
    3841Aeq = concatenate([ones(l), -ones(l)]).reshape(1,N) 
    39 Beq = array([0]) 
     42Beq = array([0.]) 
    4043lb = zeros(N) 
    4144ub = zeros(N) + 0.5 
    4245 
    43 alpha = qld.quadprog2(H, f, None, None, Aeq, Beq, lb, ub) 
    44 #alpha = 2*alpha 
     46from mystic.symbolic import linear_symbolic, solve, \ 
     47     generate_solvers as solvers, generate_constraint as constraint 
     48constrain = linear_symbolic(Aeq,Beq) 
     49constrain = constraint(solvers(solve(constrain,target=['x0']))) 
     50 
     51from mystic import supressed 
     52@supressed(1e-5) 
     53def conserve(x): 
     54    return constrain(x) 
     55 
     56from mystic.monitors import VerboseMonitor 
     57mon = VerboseMonitor(10) 
     58 
     59from mystic.solvers import diffev 
     60alpha = 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 
     64print 'solved x: ', alpha 
     65print "constraint A*x == 0: ", inner(Aeq, alpha) 
     66print "minimum 0.5*x'Hx + f'*x: ", objective(alpha, H, f) 
    4567 
    4668sv1 = SupportVectors(alpha[:l]) 
     
    4971R = RegressionFunction(x, y, alpha, svr_epsilon, pk) 
    5072 
     73print 'support vectors: ', sv1, sv2 
     74 
     75# plot data 
     76pylab.plot(x, y, 'k+', markersize=10) 
     77 
     78# plot regression function and support 
    5179xx = arange(min(x),max(x),0.1) 
    5280pylab.plot(xx,R(xx)) 
    53 pylab.plot(xx,R(xx)+svr_epsilon, 'k--') 
    54 pylab.plot(xx,R(xx)-svr_epsilon, 'k--') 
     81pylab.plot(xx,R(xx)-svr_epsilon, 'r--') 
     82pylab.plot(xx,R(xx)+svr_epsilon, 'g--') 
    5583pylab.plot(x[sv1],y[sv1],'ro') 
    5684pylab.plot(x[sv2],y[sv2],'go') 
    57  
    58 print alpha 
    59  
    6085pylab.show() 
    6186 
    62 # $Id$ 
    63 #  
    6487# end of file 
Note: See TracChangeset for help on using the changeset viewer.