LinearSVR#

  • Corresponde al modelo SVR con kernel lineal.

  • Se obtiene al reformular el problema primal como:

\min_{w,b} \frac{1}{2} w^T w + C \sum_{i=1}^n \max \left(0, |y_i - (w^T \phi(x_i)+ b)| - \epsilon\right)

  • Se usa la función de error \epsilon-insensitiva.

[1]:
from sklearn.datasets import make_regression

X, y = make_regression(n_features=4, random_state=0)
[2]:
from sklearn.svm import LinearSVR

linearSVR = LinearSVR(
    # --------------------------------------------------------------------------
    # Epsilon parameter in the epsilon-insensitive loss function. Note that the
    # value of this parameter depends on the scale of the target variable y. If
    # unsure, set epsilon=0.
    epsilon=0.0,
    # --------------------------------------------------------------------------
    # Tolerance for stopping criterion.
    tol=1e-3,
    # --------------------------------------------------------------------------
    # Regularization parameter. The strength of the regularization is inversely
    # proportional to C. Must be strictly positive.
    # penalty.
    C=1,
    # --------------------------------------------------------------------------
    # Specifies the loss function. The epsilon-insensitive loss (standard SVR)
    # is the L1 loss, while the squared epsilon-insensitive loss
    # (‘squared_epsilon_insensitive’) is the L2 loss.
    loss='epsilon_insensitive',
    # --------------------------------------------------------------------------
    # hether to calculate the intercept for this model. If set to false, no
    # intercept will be used in calculations
    fit_intercept=True,
    # --------------------------------------------------------------------------
    # When self.fit_intercept is True, instance vector x becomes
    # [x, self.intercept_scaling], i.e. a “synthetic” feature with constant
    # value equals to intercept_scaling is appended to the instance vector. The
    # intercept becomes intercept_scaling * synthetic feature weight Note! the
    # synthetic feature weight is subject to l1/l2 regularization as all other
    # features. To lessen the effect of regularization on synthetic feature
    # weight (and therefore on the intercept) intercept_scaling has to be
    # increased.
    intercept_scaling=1.0,
    # --------------------------------------------------------------------------
    # Select the algorithm to either solve the dual or primal optimization
    # problem. Prefer dual=False when n_samples > n_features.
    dual=True,
    # --------------------------------------------------------------------------
    # Controls the pseudo random number generation for shuffling the data. Pass
    # an int for reproducible output across multiple function calls
    random_state=None,
    # --------------------------------------------------------------------------
    # The maximum number of iterations to be run.
    max_iter=1000,
)

linearSVR.fit(X, y)
linearSVR.predict(X)
[2]:
array([ -30.55095759,   -0.6458061 ,  -77.1914936 ,  -13.38403813,
         18.1801038 ,  -30.12151035,   34.56371925,  -80.16935785,
        -32.35589234,   59.78179542,  -83.44132152,   67.77309488,
        -49.92473794,  -23.93571221, -111.84789107,  -44.08465841,
        -87.14806055, -110.71870437,    8.49643527,   -4.97835778,
         20.81752614,   84.59286846,   98.07396361,   73.15235246,
        -72.53155235,  -25.12048214,  200.41121381,  -54.24288837,
         28.41800609,   50.19661321,   81.68962756,   -7.23505086,
         -3.45935488,   82.877153  ,  -99.49055193,   55.22880025,
         -3.93677763,   89.27545316, -137.64285998, -208.68974901,
         67.07670989,  -19.83287988,   69.73081865,   62.0966189 ,
        -80.72307316,  -30.95893732,   88.55105755, -157.64785808,
        -43.94367411,   63.09924443,  -90.03014181,   39.28808415,
         98.56851178,  211.98062677, -104.96636582,  -51.02058039,
         37.40133741,  -49.11142001,  -53.38541583,   45.44301341,
         17.39746762,   73.58381415,  202.61309322,   23.56670239,
         62.43038472,   60.6262052 , -163.86100282,   50.31192299,
         65.12008149,  -34.33836754,   84.53070356,  -24.53157987,
       -179.18454211,  -19.65462806,  -43.23869415,  109.58684464,
        -52.97892474,  -13.88767123,  -59.76937543,  -69.6739172 ,
        -21.91120677,  -10.48687369,  -65.8454146 ,  -68.89790057,
        -36.88303111,  -14.07884056,   13.08608705,   88.63078331,
       -143.8483861 ,   98.86990346,  -82.89696107,   69.32654946,
       -101.68547961,  -27.56443293,  153.73557004, -178.86173772,
        -15.68495868,  -13.87536386,   30.9876405 ,  -82.98927737])
[3]:
linearSVR.score(X, y)
[3]:
0.8989207322892478
[4]:
linearSVR.coef_
[4]:
array([16.31719289, 26.73950553, 42.36522226, 60.46032178])
[5]:
linearSVR.intercept_
[5]:
array([-4.45333764])