sklearnsvm - juedaiyuer/researchNote GitHub Wiki

#scikit-learn里面的支持向量机#

svm是一组监督学习方法用在分类,回归,孤立点检测

它是机器学习中非常重要的监督学习模型

svm优点

  • 高维度空间的有效性

  • 维度大于样本数仍然有效

    class sklearn.svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape=None, random_state=None)

The implementation is based on libsvm. The fit time complexity is more than quadratic(二次的) with the number of samples which makes it hard to scale to dataset with more than a couple of 10000 samples.

##参数##

###C###

float, optional (default=1.0),Penalty(惩罚) parameter C of the error term.

目标函数的惩罚系数,用来平衡分类间隔margin和错分样本

###kernel###

string, optional (default=’rbf’)

Specifies(指定) the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a callable is given it is used to pre-compute the kernel matrix from data matrices; that matrix should be an array of shape (n_samples, n_samples).

###degree###

int, optional (default=3)

Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.

degree决定了多项式的最高次幂

###gamma###

float, optional (default=’auto’)

Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’. If gamma is ‘auto’ then 1/n_features will be used instead.

核函数的系数

###coef0###

float, optional (default=0.0)

Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.

核函数中的独立项

###probability###

boolean, optional (default=False)

Whether to enable probability estimates(概率估计). This must be enabled prior to calling fit, and will slow down that method.

可能性估计是否使用

###shrinking###

boolean, optional (default=True)

Whether to use the shrinking heuristic.

是否进行启发式

###tol###

float, optional (default=1e-3)

Tolerance for stopping criterion.

svm结束标准的精度

###cache_size###

float, optional

Specify the size of the kernel cache (in MB).

###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))

每个类所占据的权重,不同的类设置不同的惩罚参数C,缺省的话自适应

###verbose###

bool, default: False

Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context.

多线程有关

###max_iter###

int, optional (default=-1)

Hard limit on iterations within solver, or -1 for no limit.

最大迭代次数

###decision_function_shape###

‘ovo’, ‘ovr’ or None, default=None

Whether to return a one-vs-rest (‘ovr’) decision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). The default of None will currently behave as ‘ovo’ for backward compatibility and raise a deprecation warning, but will change ‘ovr’ in 0.19.

New in version 0.17: decision_function_shape=’ovr’ is recommended.

Changed in version 0.17: Deprecated decision_function_shape=’ovo’ and None.

###random_state###

int seed, RandomState instance, or None (default)

The seed of the pseudo random number generator to use when shuffling the data for probability estimation.

##分类##

SVC(C-Support Vector Classification):支持向量分类,基于libsvm实现的,数据拟合的时间复杂度是数据样本的二次方,这使得他很难扩展到10000个数据集,当输入是多类别时(SVM最初是处理二分类问题的),通过一对一的方案解决,当然也有别的解决办法.

###SVM解决多分类问题的方法###

SVM算法最初是为二值分类问题设计的,当处理多类问题时,就需要构造合适的多类分类器

####直接法####

直接在目标函数上进行修改,将多个分类面的参数求解合并到一个最优化问题中,通过求解该最优化问题“一次性”实现多类分类。这种方法看似简单,但其计算复杂度比较高,实现起来比较困难,只适合用于小型问题

####间接法####

主要是通过组合多个二分类器来实现多分类器的构造,常见的方法有one-against-one和one-against-all两种

####一对多法####

one-versus-rest,简称OVR SVMs

训练时依次把某个类别的样本归为一类,其他剩余的样本归为另一类,这样k个类别的样本就构造出了k个SVM。分类时将未知样本分类为具有最大分类函数值的那类

假如我有四类要划分(也就是4个Label),他们是A、B、C、D:

  • A所对应的向量作为正集,B,C,D所对应的向量作为负集
  • B所对应的向量作为正集,A,C,D所对应的向量作为负集
  • C所对应的向量作为正集,A,B,D所对应的向量作为负集
  • D所对应的向量作为正集,A,B,C所对应的向量作为负集

这种方法有种缺陷,因为训练集是1:M,这种情况下存在biased.因而不是很实用。可以在抽取数据集的时候,从完整的负集中再抽取三分之一作为训练负集

####一对一法####

one-versus-one,简称OVO SVMs或者pairwise

其做法是在任意两类样本之间设计一个SVM,因此k个类别的样本就需要设计k(k-1)/2个SVM

当对一个未知样本进行分类时,最后得票最多的类别即为该未知样本的类别

Libsvm中的多类分类就是根据这个方法实现的

这种方法虽然好,但是当类别很多的时候,model的个数是n*(n-1)/2,代价还是相当大的

####层次支持向量机####

H-SVMs

层次分类法首先将所有类别分成两个子类,再将子类进一步划分成两个次级子类,如此循环,直到得到一个单独的类别为止

官方文档上的Multi-class classification

##Multi-class classification##

在多分类中SVC和NuSVC实现了一对一的方法(Knerr et al., 1990),类别的数量n_class,构造出n_class*(n_class-1)/2个分类器,训练数据来自于每两个类别;实现one-against-one分类器,参数选项为decison_function_shape='ovo'

上面的描述在参数说明中即有

>>> X = [0], [1], [2], [3](/juedaiyuer/researchNote/wiki/0],-[1],-[2],-[3)
>>> Y = [0, 1, 2, 3]
>>> clf = svm.SVC(decision_function_shape='ovo')
>>> clf.fit(X, Y) 
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovo', degree=3, gamma='auto', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
>>> dec = clf.decision_function([1](/juedaiyuer/researchNote/wiki/1))
>>> dec.shape[1] # 4 classes: 4*3/2 = 6
6
>>> clf.decision_function_shape = "ovr"
>>> dec = clf.decision_function([1](/juedaiyuer/researchNote/wiki/1))
>>> dec.shape[1] # 4 classes
4

LinearSVC实现了另一种的多分类策略,所谓的多分类SVM通过crammer和singer参数制定,用multi_class='crammer_singer'这个选项。这个方法是连续的,对于one-vs-rest分类方法不是很正确,在实践中,one-vs-rest分类方法通常是首选,因为结果大多相似,但是运行时间明显减少

对one-vs-rest分类,LinearSVC的参数coef_和intercept_,分别对应模型[n_class,n_features]. 每一行的参数对应于众多的one-vs-rest分类器中n_class中的一个特征

##source##