티스토리 뷰

2장_05_LinearSVM

선형 서포트벡터 머신 (Linear SVM)

  • SVM 은 클래스를 구분하는 분류 문제에서, 각 클래스를 잘 구분하는 선을 그어주는 방식이다.
  • 아래의 그림들을 보면 SVM 이 직관적이라는 것을 알 수 있다. 두 클래스의 가운데 선을 그어주게 된다. 가장 가까이 있는 점들과의 거리가 가장 큰 직선을 찾는다.
  • 이때 가장 가까이 있는 점들을 Support Vector 라고 하고, 찾은 직선과 서포트벡터 사이의 거리를 최대 마진(margin) 이라 한다.
  • 결국 마진을 최대로 하는 서포트벡터와 직선을 찾는 것이 목표이다.
  • SVM 에 대한 상세한 설명은 뒤에서 다루겠다.
  • 참고 자료 : https://en.wikipedia.org/wiki/Support_vector_machine
  • 아래에서, Iris 데이터에 선형 SVM 을 적용해 보겠다.
  • 선형 SVM 은 직선(또는 평면)으로 클래스를 구분한다.
In [3]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

iris = load_iris()
In [4]:
from sklearn.model_selection import train_test_split

col1 = 0
col2 = 1

X = iris.data[:,[col1,col2]] # 시각화를 위해 속성 2개만 선정 (petal length & petal width)
# y = iris.target
y = iris.target.copy()
y[y==2] = 1 # 타겟값을 setosa(0), others(1) 로 설정

X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
Out[4]:
((112, 2), (38, 2), (112,), (38,))
In [5]:
plt.figure(figsize=[10,8])
plt.scatter(X[:,0], X[:,1], c=y)
plt.colorbar()
Out[5]:
<matplotlib.colorbar.Colorbar at 0x1749c668400>
In [6]:
from sklearn.svm import LinearSVC

model = LinearSVC(C=1)
model.fit(X_train, y_train)

score = model.score(X_train, y_train)
print(score)
0.9910714285714286
In [7]:
display(model.coef_, model.intercept_)
array([[ 1.39517014, -2.05735663]])
array([-1.13092534])
In [8]:
import mglearn

plt.figure(figsize=[10,8])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='spring')
mglearn.discrete_scatter(X_train[:,0], X_train[:,1], y_train)
Out[8]:
[<matplotlib.lines.Line2D at 0x1749e0aac50>,
 <matplotlib.lines.Line2D at 0x1749e0aad68>]
In [9]:
scale = 300
xmax = X_train[:,0].max()+1
xmin = X_train[:,0].min()-1
ymax = X_train[:,1].max()+1
ymin = X_train[:,1].min()-1

xx = np.linspace(xmin,xmax,scale)
yy = np.linspace(ymin,ymax,scale)
data1, data2 = np.meshgrid(xx,yy)
X_grid = np.c_[data1.ravel(), data2.ravel()]
pred_y = model.predict(X_grid)

fig=plt.figure(figsize=[12,10])

CS = plt.imshow(pred_y.reshape(scale,scale), interpolation=None, origin='lower',
                extent=[xmin,xmax,ymin,ymax], alpha=0.3, cmap='gray_r')

# draw X_train
plt.scatter(X_train[:,0], X_train[:,1], c=y_train, s=60)

plt.xlabel(iris.feature_names[col1])
plt.ylabel(iris.feature_names[col2])
plt.colorbar(CS, shrink=0.3)
plt.title('Linear SVC - Iris',fontsize=20)
Out[9]:
Text(0.5,1,'Linear SVC - Iris')
In [10]:
w = model.coef_
b = model.intercept_

display(w, b)
array([[ 1.39517014, -2.05735663]])
array([-1.13092534])
  • 선형 SVM 은 평면으로 클래스를 구분하므로 기울기와 편향값을 제공한다.
  • 하지만 확률수치를 알 수 있는 model.predict_proba() 는 제공하지 않지만, decision_function() 을 제공한다.
  • LinearSVC 는 C 라는 중요한 옵션을 가진다. C 값이 클수록 모델이 훈련데이터에 과대적합 되는 경향이 생긴다. 상세한 설명은 뒤에서 다루겠다.
  • penalty는 선 밖에 있는 점의 정의 방법이다.
In [11]:
help(LinearSVC)
Help on class LinearSVC in module sklearn.svm.classes:

class LinearSVC(sklearn.base.BaseEstimator, sklearn.linear_model.base.LinearClassifierMixin, sklearn.linear_model.base.SparseCoefMixin)
 |  Linear Support Vector Classification.
 |  
 |  Similar to SVC with parameter kernel='linear', but implemented in terms of
 |  liblinear rather than libsvm, so it has more flexibility in the choice of
 |  penalties and loss functions and should scale better to large numbers of
 |  samples.
 |  
 |  This class supports both dense and sparse input and the multiclass support
 |  is handled according to a one-vs-the-rest scheme.
 |  
 |  Read more in the :ref:`User Guide <svm_classification>`.
 |  
 |  Parameters
 |  ----------
 |  penalty : string, 'l1' or 'l2' (default='l2')
 |      Specifies the norm used in the penalization. The 'l2'
 |      penalty is the standard used in SVC. The 'l1' leads to ``coef_``
 |      vectors that are sparse.
 |  
 |  loss : string, 'hinge' or 'squared_hinge' (default='squared_hinge')
 |      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.
 |  
 |  dual : bool, (default=True)
 |      Select the algorithm to either solve the dual or primal
 |      optimization problem. Prefer dual=False when n_samples > n_features.
 |  
 |  tol : float, optional (default=1e-4)
 |      Tolerance for stopping criteria.
 |  
 |  C : float, optional (default=1.0)
 |      Penalty parameter C of the error term.
 |  
 |  multi_class : string, 'ovr' or 'crammer_singer' (default='ovr')
 |      Determines the multi-class strategy if `y` contains more than
 |      two classes.
 |      ``"ovr"`` trains n_classes one-vs-rest classifiers, while
 |      ``"crammer_singer"`` optimizes a joint objective over all classes.
 |      While `crammer_singer` is interesting from a theoretical perspective
 |      as it is consistent, it is seldom used in practice as it rarely leads
 |      to better accuracy and is more expensive to compute.
 |      If ``"crammer_singer"`` is chosen, the options loss, penalty and dual
 |      will be ignored.
 |  
 |  fit_intercept : boolean, optional (default=True)
 |      Whether to calculate the intercept for this model. If set
 |      to false, no intercept will be used in calculations
 |      (i.e. data is expected to be already centered).
 |  
 |  intercept_scaling : float, optional (default=1)
 |      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.
 |  
 |  class_weight : {dict, 'balanced'}, optional
 |      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))``
 |  
 |  verbose : int, (default=0)
 |      Enable verbose output. Note that this setting takes advantage of a
 |      per-process runtime setting in liblinear that, if enabled, may not work
 |      properly in a multithreaded context.
 |  
 |  random_state : int, RandomState instance or None, optional (default=None)
 |      The seed of the pseudo random number generator to use when shuffling
 |      the data.  If int, random_state is the seed used by the random number
 |      generator; If RandomState instance, random_state is the random number
 |      generator; If None, the random number generator is the RandomState
 |      instance used by `np.random`.
 |  
 |  max_iter : int, (default=1000)
 |      The maximum number of iterations to be run.
 |  
 |  Attributes
 |  ----------
 |  coef_ : array, shape = [n_features] if n_classes == 2 else [n_classes, n_features]
 |      Weights assigned to the features (coefficients in the primal
 |      problem). This is only available in the case of a linear kernel.
 |  
 |      ``coef_`` is a readonly property derived from ``raw_coef_`` that
 |      follows the internal memory layout of liblinear.
 |  
 |  intercept_ : array, shape = [1] if n_classes == 2 else [n_classes]
 |      Constants in decision function.
 |  
 |  Examples
 |  --------
 |  >>> from sklearn.svm import LinearSVC
 |  >>> from sklearn.datasets import make_classification
 |  >>> X, y = make_classification(n_features=4, random_state=0)
 |  >>> clf = LinearSVC(random_state=0)
 |  >>> clf.fit(X, y)
 |  LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
 |       intercept_scaling=1, loss='squared_hinge', max_iter=1000,
 |       multi_class='ovr', penalty='l2', random_state=0, tol=0.0001,
 |       verbose=0)
 |  >>> print(clf.coef_)
 |  [[ 0.08551385  0.39414796  0.49847831  0.37513797]]
 |  >>> print(clf.intercept_)
 |  [ 0.28418066]
 |  >>> print(clf.predict([[0, 0, 0, 0]]))
 |  [1]
 |  
 |  Notes
 |  -----
 |  The underlying C implementation uses a random number generator to
 |  select features when fitting the model. It is thus not uncommon
 |  to have slightly different results for the same input data. If
 |  that happens, try with a smaller ``tol`` parameter.
 |  
 |  The underlying implementation, liblinear, uses a sparse internal
 |  representation for the data that will incur a memory copy.
 |  
 |  Predict output may not match that of standalone liblinear in certain
 |  cases. See :ref:`differences from liblinear <liblinear_differences>`
 |  in the narrative documentation.
 |  
 |  References
 |  ----------
 |  `LIBLINEAR: A Library for Large Linear Classification
 |  <http://www.csie.ntu.edu.tw/~cjlin/liblinear/>`__
 |  
 |  See also
 |  --------
 |  SVC
 |      Implementation of Support Vector Machine classifier using libsvm:
 |      the kernel can be non-linear but its SMO algorithm does not
 |      scale to large number of samples as LinearSVC does.
 |  
 |      Furthermore SVC multi-class mode is implemented using one
 |      vs one scheme while LinearSVC uses one vs the rest. It is
 |      possible to implement one vs the rest with SVC by using the
 |      :class:`sklearn.multiclass.OneVsRestClassifier` wrapper.
 |  
 |      Finally SVC can fit dense data without memory copy if the input
 |      is C-contiguous. Sparse data will still incur memory copy though.
 |  
 |  sklearn.linear_model.SGDClassifier
 |      SGDClassifier can optimize the same cost function as LinearSVC
 |      by adjusting the penalty and loss parameters. In addition it requires
 |      less memory, allows incremental (online) learning, and implements
 |      various loss functions and regularization regimes.
 |  
 |  Method resolution order:
 |      LinearSVC
 |      sklearn.base.BaseEstimator
 |      sklearn.linear_model.base.LinearClassifierMixin
 |      sklearn.base.ClassifierMixin
 |      sklearn.linear_model.base.SparseCoefMixin
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, penalty='l2', loss='squared_hinge', dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None, max_iter=1000)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  fit(self, X, y, sample_weight=None)
 |      Fit the model according to the given training data.
 |      
 |      Parameters
 |      ----------
 |      X : {array-like, sparse matrix}, shape = [n_samples, n_features]
 |          Training vector, where n_samples in the number of samples and
 |          n_features is the number of features.
 |      
 |      y : array-like, shape = [n_samples]
 |          Target vector relative to X
 |      
 |      sample_weight : array-like, shape = [n_samples], optional
 |          Array of weights that are assigned to individual
 |          samples. If not provided,
 |          then each sample is given unit weight.
 |      
 |      Returns
 |      -------
 |      self : object
 |          Returns self.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from sklearn.base.BaseEstimator:
 |  
 |  __getstate__(self)
 |  
 |  __repr__(self)
 |      Return repr(self).
 |  
 |  __setstate__(self, state)
 |  
 |  get_params(self, deep=True)
 |      Get parameters for this estimator.
 |      
 |      Parameters
 |      ----------
 |      deep : boolean, optional
 |          If True, will return the parameters for this estimator and
 |          contained subobjects that are estimators.
 |      
 |      Returns
 |      -------
 |      params : mapping of string to any
 |          Parameter names mapped to their values.
 |  
 |  set_params(self, **params)
 |      Set the parameters of this estimator.
 |      
 |      The method works on simple estimators as well as on nested objects
 |      (such as pipelines). The latter have parameters of the form
 |      ``<component>__<parameter>`` so that it's possible to update each
 |      component of a nested object.
 |      
 |      Returns
 |      -------
 |      self
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from sklearn.base.BaseEstimator:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from sklearn.linear_model.base.LinearClassifierMixin:
 |  
 |  decision_function(self, X)
 |      Predict confidence scores for samples.
 |      
 |      The confidence score for a sample is the signed distance of that
 |      sample to the hyperplane.
 |      
 |      Parameters
 |      ----------
 |      X : {array-like, sparse matrix}, shape = (n_samples, n_features)
 |          Samples.
 |      
 |      Returns
 |      -------
 |      array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
 |          Confidence scores per (sample, class) combination. In the binary
 |          case, confidence score for self.classes_[1] where >0 means this
 |          class would be predicted.
 |  
 |  predict(self, X)
 |      Predict class labels for samples in X.
 |      
 |      Parameters
 |      ----------
 |      X : {array-like, sparse matrix}, shape = [n_samples, n_features]
 |          Samples.
 |      
 |      Returns
 |      -------
 |      C : array, shape = [n_samples]
 |          Predicted class label per sample.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from sklearn.base.ClassifierMixin:
 |  
 |  score(self, X, y, sample_weight=None)
 |      Returns the mean accuracy on the given test data and labels.
 |      
 |      In multi-label classification, this is the subset accuracy
 |      which is a harsh metric since you require for each sample that
 |      each label set be correctly predicted.
 |      
 |      Parameters
 |      ----------
 |      X : array-like, shape = (n_samples, n_features)
 |          Test samples.
 |      
 |      y : array-like, shape = (n_samples) or (n_samples, n_outputs)
 |          True labels for X.
 |      
 |      sample_weight : array-like, shape = [n_samples], optional
 |          Sample weights.
 |      
 |      Returns
 |      -------
 |      score : float
 |          Mean accuracy of self.predict(X) wrt. y.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from sklearn.linear_model.base.SparseCoefMixin:
 |  
 |  densify(self)
 |      Convert coefficient matrix to dense array format.
 |      
 |      Converts the ``coef_`` member (back) to a numpy.ndarray. This is the
 |      default format of ``coef_`` and is required for fitting, so calling
 |      this method is only required on models that have previously been
 |      sparsified; otherwise, it is a no-op.
 |      
 |      Returns
 |      -------
 |      self : estimator
 |  
 |  sparsify(self)
 |      Convert coefficient matrix to sparse format.
 |      
 |      Converts the ``coef_`` member to a scipy.sparse matrix, which for
 |      L1-regularized models can be much more memory- and storage-efficient
 |      than the usual numpy.ndarray representation.
 |      
 |      The ``intercept_`` member is not converted.
 |      
 |      Notes
 |      -----
 |      For non-sparse models, i.e. when there are not many zeros in ``coef_``,
 |      this may actually *increase* memory usage, so use this method with
 |      care. A rule of thumb is that the number of zero elements, which can
 |      be computed with ``(coef_ == 0).sum()``, must be more than 50% for this
 |      to provide significant benefits.
 |      
 |      After calling this method, further fitting with the partial_fit
 |      method (if any) will not work until you call densify.
 |      
 |      Returns
 |      -------
 |      self : estimator

In [12]:
mglearn.plots.plot_2d_separator(model, X_train, eps=0.5)
#mglearn.plots.plot_2d_classification(model, X_train, eps=0.5)
mglearn.discrete_scatter(X_test[:,0], X_test[:,1], y_test)
Out[12]:
[<matplotlib.lines.Line2D at 0x1749e4176d8>,
 <matplotlib.lines.Line2D at 0x1749e417940>]
In [13]:
help(model.decision_function)
Help on method decision_function in module sklearn.linear_model.base:

decision_function(X) method of sklearn.svm.classes.LinearSVC instance
    Predict confidence scores for samples.
    
    The confidence score for a sample is the signed distance of that
    sample to the hyperplane.
    
    Parameters
    ----------
    X : {array-like, sparse matrix}, shape = (n_samples, n_features)
        Samples.
    
    Returns
    -------
    array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
        Confidence scores per (sample, class) combination. In the binary
        case, confidence score for self.classes_[1] where >0 means this
        class would be predicted.

In [14]:
model.classes_
Out[14]:
array([0, 1])
In [15]:
model.decision_function(X_test)
Out[15]:
array([ 1.40619861, -1.07678878, -0.73861582,  0.92850864,  1.20046295,
       -0.59201909, -1.20922607,  1.60485455,  0.92850864,  1.47241726,
        1.61901399,  1.21462238,  1.81059022,  1.40619861, -1.57571791,
        0.71569326,  1.3541394 ,  1.20754267,  1.76561072,  1.91220746,
        1.06094593,  2.71391095,  2.05172447, -0.60617852,  0.23092356,
       -0.59201909,  2.04464475,  0.92142892,  0.76775247,  0.245083  ,
       -0.67239717, -1.34874309,  2.59563309,  1.42035805,  3.43273518,
       -1.83351278, -1.56155847,  1.11300515])
In [16]:
model.predict(X_test)
Out[16]:
array([1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
       1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1])

할 일

모든 속성과 모든 품종 사용

In [17]:
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target)
model = LinearSVC(C=1)
model.fit(X_train, y_train)

model.score(X_test, y_test)
Out[17]:
0.868421052631579
In [18]:
model.coef_
Out[18]:
array([[ 0.1842335 ,  0.45122583, -0.80794305, -0.4507152 ],
       [ 0.03877774, -1.02961359,  0.51036833, -1.17378361],
       [-0.89732685, -0.61450123,  1.22419345,  1.72715798]])
In [19]:
model.intercept_
Out[19]:
array([ 0.10956081,  2.05243171, -1.54725408])
In [20]:
pred_y = model.predict(X_test)
pred_y
Out[20]:
array([1, 2, 0, 1, 2, 2, 2, 0, 1, 0, 0, 1, 0, 2, 1, 1, 1, 2, 0, 2, 2, 2,
       0, 1, 1, 2, 1, 2, 1, 2, 0, 0, 1, 2, 0, 2, 0, 1])
In [21]:
y_test
Out[21]:
array([1, 2, 0, 1, 1, 1, 2, 0, 1, 0, 0, 1, 0, 2, 1, 1, 2, 2, 0, 2, 2, 2,
       0, 1, 1, 2, 2, 2, 2, 2, 0, 0, 1, 2, 0, 2, 0, 1])
In [22]:
np.where(y_test!=pred_y)
Out[22]:
(array([ 4,  5, 16, 26, 28], dtype=int64),)
In [23]:
plt.scatter(X_test[13,0], X_test[13,1], s=200)
plt.scatter(X_test[33,0], X_test[33,1], s=200)
plt.scatter(X_test[:,0], X_test[:,1], c=y_test)
Out[23]:
<matplotlib.collections.PathCollection at 0x1749e45ca90>
In [24]:
plt.scatter(X_test[13,2], X_test[13,3], s=200)
plt.scatter(X_test[33,2], X_test[33,3], s=200)
plt.scatter(X_test[:,2], X_test[:,3], c=y_test)
Out[24]:
<matplotlib.collections.PathCollection at 0x1749e4b8588>
In [25]:
model.decision_function(X_test)
Out[25]:
array([[-1.7882998 , -0.16479923, -0.61366125],
       [-2.80179786, -1.16297279,  1.71094506],
       [ 1.15411895, -0.30118537, -5.84010348],
       [-1.57923865, -0.45817221, -1.15694235],
       [-1.6077584 , -0.79698438, -0.74819606],
       [-1.85371731, -0.29102723, -0.13671526],
       [-3.41551314,  0.16178711,  1.49626671],
       [ 1.66061483, -1.31916564, -6.72380277],
       [-1.75919954, -0.58031604, -0.7111053 ],
       [ 1.00982978, -0.37140467, -5.45523565],
       [ 1.14519753, -0.68028875, -5.63958602],
       [-1.37743162,  0.39482756, -1.43430928],
       [ 1.47189968, -1.01694302, -6.48475184],
       [-2.67416258, -0.43064024,  0.86751539],
       [-1.88334697,  0.23967318, -1.07553036],
       [-1.8554217 , -0.13167465, -1.05447828],
       [-2.74891003,  0.82674614,  0.65485343],
       [-2.75698166,  0.05610417,  0.98190089],
       [ 1.34480782, -0.81877585, -6.18238623],
       [-2.38158095, -0.31755245,  0.66728478],
       [-2.58044357, -0.12992451,  1.11408363],
       [-3.16197495, -0.72436751,  1.21006818],
       [ 1.17969427, -0.58419525, -5.5820591 ],
       [-1.3453359 ,  0.79959875, -1.25105573],
       [-1.73067979, -0.24150386, -1.1198516 ],
       [-2.17417315, -0.25310691,  0.15372894],
       [-2.50812941,  0.81111445,  0.42858634],
       [-2.28166669, -0.09523094,  0.24786572],
       [-2.6174945 ,  0.32487316,  0.01226369],
       [-3.01688707, -0.40654142,  1.67668728],
       [ 1.15411895, -0.30118537, -5.84010348],
       [ 1.08122428, -0.48789534, -5.46590842],
       [-1.79854939,  0.16128553, -0.62844466],
       [-2.27093872, -0.73496525,  0.35142002],
       [ 1.04688073, -1.24500801, -5.18963832],
       [-2.6533893 , -0.09629476,  1.37701302],
       [ 1.79673025, -1.57890887, -7.00415449],
       [-1.51921765, -0.42474886, -0.95263672]])

두 속성과 모든 품종 사용

In [26]:
col1 = 1
col2 = 2

X = iris.data[:,[col1,col2]] # 시각화를 위해 속성 2개만 선정 (petal length & petal width)
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y)
model = LinearSVC(C=10)
model.fit(X_train, y_train)

model.score(X_test, y_test)
Out[26]:
0.9210526315789473
In [27]:
plt.figure(figsize=[8,6])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='ocean')
mglearn.discrete_scatter(X_train[:,0], X_train[:,1], y_train)
Out[27]:
[<matplotlib.lines.Line2D at 0x1749e50ce80>,
 <matplotlib.lines.Line2D at 0x1749e50cf98>,
 <matplotlib.lines.Line2D at 0x1749e5164e0>]
In [28]:
plt.figure(figsize=[10,8])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='ocean')
mglearn.discrete_scatter(X_test[:,0], X_test[:,1], y_test)
Out[28]:
[<matplotlib.lines.Line2D at 0x1749e547320>,
 <matplotlib.lines.Line2D at 0x1749e547438>,
 <matplotlib.lines.Line2D at 0x1749e547940>]

C 값 변경

In [29]:
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target)

score1 = []
score2 = []
Cs = [0.001, 0.01, 1, 10, 100, 1000]

for C in Cs:
    model = LinearSVC(C=C)
    model.fit(X_train, y_train)
    s1 = model.score(X_train, y_train)
    s2 = model.score(X_test, y_test)
    score1.append(s1)
    score2.append(s2)
    
plt.plot(score1, 'b^:')
plt.plot(score2, 'ro-')
plt.legend(['train','test'])
plt.xticks(range(len(Cs)), Cs)
Out[29]:
([<matplotlib.axis.XTick at 0x1749e565668>,
  <matplotlib.axis.XTick at 0x1749e560f60>,
  <matplotlib.axis.XTick at 0x1749e560cc0>,
  <matplotlib.axis.XTick at 0x1749e59a208>,
  <matplotlib.axis.XTick at 0x1749e59a0b8>,
  <matplotlib.axis.XTick at 0x1749e59a898>],
 <a list of 6 Text xticklabel objects>)

'beginner > 파이썬 머신러닝 기초' 카테고리의 다른 글

지도학습 - 로지스틱회귀  (0) 2019.03.05
지도학습 - LinearSVM_2  (0) 2019.03.05
1월 지하철 승하차 인원 분석  (0) 2019.02.28
지도학습 - 선형회귀  (1) 2019.02.27
와인 데이터 분석  (0) 2019.02.26
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함