LinearSVC#
Corresponde al modelo SVC con kernel lineal.
Sus parámetros se calculan solucionando la ecuación:
\min_{w,b} \frac{1}{2} w^T w + C \sum_{i=1}^n \max \left(0, 1-y_i \left( w^T \phi(x_i)+ b \right) \right)
Se usa la función de hinge (o hinge2) como métrica de error.
[1]:
from sklearn.datasets import make_blobs
NPOINTS = 150
X, d = make_blobs(
n_samples=NPOINTS,
n_features=2,
centers=3,
cluster_std=0.8,
shuffle=False,
random_state=12345,
)
[2]:
from sklearn.svm import LinearSVC
linearSVC = LinearSVC(
# --------------------------------------------------------------------------
# Specifies the norm used in the penalization {'l1', 'l2'}. The ‘l2’
# penalty is the standard used in SVC.
penalty='l2',
# --------------------------------------------------------------------------
# Specifies the loss function. ‘hinge’ is the standard SVM loss (used e.g.
# by the SVC class) while ‘squared_hinge’ is the square of the hinge loss.
# The combination of penalty='l1' and loss='hinge' is not supported.
loss='hinge',
# --------------------------------------------------------------------------
# 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,
# --------------------------------------------------------------------------
# Whether 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,
# --------------------------------------------------------------------------
# Set the parameter C of class i to class_weight[i]*C for SVC. If not
# given, all classes are supposed to have weight one. The “balanced” mode
# uses the values of y to automatically adjust weights inversely
# proportional to class frequencies in the input data as
# n_samples / (n_classes * np.bincount(y)).
class_weight=None,
# --------------------------------------------------------------------------
# The maximum number of iterations to be run.
max_iter=1000,
)
linearSVC.fit(X, d)
linearSVC.predict(X)
[2]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
[3]:
linearSVC.score(X, d)
[3]:
1.0
[4]:
linearSVC.coef_
[4]:
array([[ 0.23076305, -0.21562713],
[-0.33610822, -0.30659105],
[-0.07930195, 0.56635286]])
[5]:
linearSVC.intercept_
[5]:
array([-1.34329076, -0.70744767, 0.79081926])
[6]:
linearSVC.classes_
[6]:
array([0, 1, 2])