机器学习——python scikit-learn SVC分类

scikit-learn SVC例子:https://scikit-learn.org/stable/modules/svm.html

SVM可以用于分类、回归、异常检测。SVM库中包括SVC、LinearSVC接口

1.SVC函数介绍

​ clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')
    clf.fit(x_train, y_train.ravel())​

C:惩罚参数

            默认值为1,惩罚参数是由于松弛变量而加入的,它表征的是对错误分类的惩罚程度,也就是不允许分类出错的程度。C越大,表明越不允许分类出错,但是C越大越可能过拟合。C太小的话趋于0的话,分类将不会关注分类是否正确的问题,只要求间隔 越大越好,此时分类也没有意义。

Kernel:核函数

作用:核函数的引入是为了解决线性不可分的问题,讲分类点映射的高维空间中以后,转化为可线性分割的问题

  1. kernel=‘linear’时,为线性核,C越大分类效果越好,但可能会过拟合;
  2. kernel='rbf'时,为高斯核,gamma值越小,分类界面越连续;gamma值越大,分类界面越“散”,分类效果越好,但可能会过拟合;
  3. kernel='poly':多项式核
  4. kernel=sigmoid’:Sigmoid核函数 
  5. kernel=‘precomputed’:核矩阵

decision_function_shape参数

  1. decision_function_shape='ovr'时,为one v rest,即一个类别与其他ov类别进行划分;
  2. decision_function_shape='ovo'时,为one v one,即将类别两两进行划分,用二分类的方法模拟多分类的结果;

degree 参数

                多项式poly函数的维度,默认是3,选择其他核函数时会被忽略

gamma 参数

              ‘rbf’,‘poly’ 和‘sigmoid’的核函数参数。默认是’auto’,则会选择1/n_features

coef0参数

          核函数的常数项。对于‘poly’和‘sigmoid’有用

probability参数

              是否采用概率估计?默认为False

shrinking参数

          是否采用shirinking heuristic方法,默认为true

tol参数

        停止训练的误差值大小,默认为1e-3

cache_size参数

          核函数cache缓存大小,默认为200

class_weight参数

        类别的权重,字典形式传递,设置第几类的参数C为weight*C(C-SVC中的C)

verbose参数

        允许冗余输出

max_iter参数

         最大迭代册数,-1为无限制

random_state参数

      数据洗牌时的种子值,int值

2.例子

"""
==================================================
Plot different SVM classifiers in the iris dataset
==================================================

Comparison of different linear SVM classifiers on a 2D projection of the iris
dataset. We only consider the first 2 features of this dataset:

- Sepal length
- Sepal width

This example shows how to plot the decision surface for four SVM classifiers
with different kernels.

The linear models ``LinearSVC()`` and ``SVC(kernel='linear')`` yield slightly
different decision boundaries. This can be a consequence of the following
differences:

- ``LinearSVC`` minimizes the squared hinge loss while ``SVC`` minimizes the
  regular hinge loss.

- ``LinearSVC`` uses the One-vs-All (also known as One-vs-Rest) multiclass
  reduction while ``SVC`` uses the One-vs-One multiclass reduction.

Both linear models have linear decision boundaries (intersecting hyperplanes)
while the non-linear kernel models (polynomial or Gaussian RBF) have more
flexible non-linear decision boundaries with shapes that depend on the kind of
kernel and its parameters.

.. NOTE:: while plotting the decision function of classifiers for toy 2D
   datasets can help get an intuitive understanding of their respective
   expressive power, be aware that those intuitions don't always generalize to
   more realistic high-dimensional problems.

"""
print(__doc__)

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets


def make_meshgrid(x, y, h=.02):
    """Create a mesh of points to plot in

    Parameters
    ----------
    x: data to base x-axis meshgrid on
    y: data to base y-axis meshgrid on
    h: stepsize for meshgrid, optional

    Returns
    -------
    xx, yy : ndarray
    """
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    return xx, yy


def plot_contours(ax, clf, xx, yy, **params):
    """Plot the decision boundaries for a classifier.

    Parameters
    ----------
    ax: matplotlib axes object
    clf: a classifier
    xx: meshgrid ndarray
    yy: meshgrid ndarray
    params: dictionary of params to pass to contourf, optional
    """
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out


# import some data to play with
iris = datasets.load_iris()
# Take the first two features. We could avoid this by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
models = (svm.SVC(kernel='linear', C=C),
          svm.LinearSVC(C=C),
          svm.SVC(kernel='rbf', gamma=0.7, C=C),
          svm.SVC(kernel='poly', degree=3, C=C))
models = (clf.fit(X, y) for clf in models)

# title for the plots
titles = ('SVC with linear kernel',
          'LinearSVC (linear kernel)',
          'SVC with RBF kernel',
          'SVC with polynomial (degree 3) kernel')

# Set-up 2x2 grid for plotting.
plt.figure(1)
fig, sub = plt.subplots(2, 2)
plt.subplots_adjust(wspace=0.4, hspace=0.4)

X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)

for clf, title, ax in zip(models, titles, sub.flatten()):
    plot_contours(ax, clf, xx, yy,
                  cmap=plt.cm.coolwarm, alpha=0.8)
    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xlabel('Sepal length')
    ax.set_ylabel('Sepal width')
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)

plt.show()

3.结果

机器学习——python scikit-learn SVC分类_第1张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(python,机器学习)