main content

discard support vectors for linear support vector machine (svm) regression model -凯发k8网页登录

class: classreg.learning.regr.compactregressionsvm, regressionsvm
package: classreg.learning.regr

discard support vectors for linear support vector machine (svm) regression model

syntax

mdlout = discardsupportvectors(mdl)

description

mdlout = discardsupportvectors(mdl) returns the trained, linear support vector machine (svm) regression model mdlout, which is similar to the trained, linear svm regression model mdl, except:

  • the alpha and supportvectors properties are empty ([]).

  • if you display mdlout, the software lists the beta property instead of the alpha property.

input arguments

trained, linear svm regression model, specified as a regressionsvm or compactregressionsvm model.

if you train the model using a kernel function that is not linear (i.e., if the field mdl.kernelfunction is something other than 'linear'), the software returns an error. you can only discard support vectors for linear models.

output arguments

trained, linear svm regression model, returned as a regressionsvm or compactregressionsvm model. mdlout is the same type as mdl.

after discarding the support vectors, the properties alpha and supportvectors are empty ([]). the software lists the property beta in its display, and does not list the property alpha. the and methods compute predicted responses using the coefficients stored in the beta property.

examples

this model shows how to reduce the disk space used by a trained, linear svm regression model by discarding the support vectors and other related parameters.

load the carsmall data set. specify horsepower and weight as the predictor variables (x), and mpg as the response variable (y).

load carsmall
x = [horsepower,weight];
y = mpg;

train a linear svm regression model, standardizing the data. display the number of support vectors.

mdl = fitrsvm(x,y,'standardize',true)
numsv = size(mdl.supportvectors,1)
mdl = 
  regressionsvm
           predictornames: {'x1'  'x2'}
             responsename: 'y'
    categoricalpredictors: []
        responsetransform: 'none'
                    alpha: [77x1 double]
                     bias: 22.9131
         kernelparameters: [1x1 struct]
                       mu: [109.3441 2.9625e 03]
                    sigma: [45.3545 805.9668]
          numobservations: 93
           boxconstraints: [93x1 double]
          convergenceinfo: [1x1 struct]
          issupportvector: [93x1 logical]
                   solver: 'smo'
  properties, methods
numsv =
    77

by default, fitrsvm trains a linear svm regression model. the software lists alpha in the display. the model has 77 support vectors.

note that the predictor and response variables contain several nan values. when training a model, fitrsvm will remove rows that contain nan values from both the predictor and response data. as a result, the trained model uses only 93 of the 100 total observations contained in the sample data.

discard the support vectors and other related parameters.

mdlout = discardsupportvectors(mdl)
mdlout.alpha
mdlout.supportvectors
mdlout = 
  regressionsvm
           predictornames: {'x1'  'x2'}
             responsename: 'y'
    categoricalpredictors: []
        responsetransform: 'none'
                     beta: [2x1 double]
                     bias: 22.9131
         kernelparameters: [1x1 struct]
                       mu: [109.3441 2.9625e 03]
                    sigma: [45.3545 805.9668]
          numobservations: 93
           boxconstraints: [93x1 double]
          convergenceinfo: [1x1 struct]
          issupportvector: [93x1 logical]
                   solver: 'smo'
  properties, methods
ans =
     []
ans =
     []

the software lists beta in the display instead of alpha. the alpha and supportvectors properties are empty.

compare the sizes of the models.

vars = whos('mdl','mdlout');
[vars(1).bytes,vars(2).bytes]
ans =
       15004       13156

mdlout consumes less memory than mdl because it does not store the support vectors.

this example shows how to reduce the memory consumption of a full, trained svm regression model by compacting the model and discarding the support vectors.

load the carsmall sample data.

load carsmall
rng default  % for reproducibility

train a linear svm regression model using weight as the predictor variable and mpg as the response variable. standardize the data.

mdl = fitrsvm(weight,mpg,'standardize',true);

note that mpg contains several nan values. when training a model, fitrsvm will remove rows that contain nan values from both the predictor and response data. as a result, the trained model uses only 94 of the 100 total observations contained in the sample data.

compact the regression model to discard the training data and some information related to the training process.

compactmdl = compact(mdl);

compactmdl is a compactregressionsvm model that has the same parameters, support vectors, and related estimates as mdl, but no longer stores the training data.

discard the support vectors and related estimates for the compacted model.

mdlout = discardsupportvectors(compactmdl);

mdlout is a compactregressionsvm model that has the same parameters as mdl and compactmdl, but no longer stores the support vectors and related estimates.

compare the sizes of the three svm regression models, compactmdl, mdl, and mdlout.

vars = whos('compactmdl','mdl','mdlout');
[vars(1).bytes,vars(2).bytes,vars(3).bytes]
ans =
        3601       13727        2305

the compacted model compactmdl consumes 3601 bytes of memory, while the full model mdl consumes 13727 bytes of memory. the model mdlout, which also discards the support vectors, consumes 2305 bytes of memory.

tips

for a trained, linear svm regression model, the supportvectors property is an nsv-by-p matrix. nsv is the number of support vectors (at most the training sample size) and p is the number of predictor variables. if any of the predictors are categorical, then p includes the number of dummy variables necessary to account for all of the categorical predictor levels. the alpha property is a vector with nsv elements.

the supportvectors and alpha properties can be large for complex data sets that contain many observations or examples. however, the beta property is a vector with p elements, which may be considerably smaller. you can use a trained svm regression model to predict response values even if you discard the support vectors because the and methods use beta to compute the predicted responses.

if the trained, linear svm regression model has many support vectors, use discardsupportvectors to reduce the amount of disk space that the trained, linear svm regression model consumes. you can display the size of the support vector matrix by entering size(mdlin.supportvectors).

algorithms

the and estimate response values using the formula

f(x)=(xs)β β0,

where:

  • β is the beta value, stored as mdl.beta.

  • β0 is the bias value, stored as mdl.bias.

  • x is the training data.

  • s is the kernel scale value, stored as mdl.kernelparameters.scale.

in this way, the software can use the value of mdl.beta to make predictions even after discarding the support vectors.

extended capabilities

version history

introduced in r2015b
网站地图