一:SVM的基本原理:
SVM用于进行二分类问题,对于一组二分类训练集, , i=1,..., n, ;SVM求解如下的优化问题
以上问题是二次规划问题,可以通过拉格朗日乘子法将其转化为一个对偶问题,
二:sk-learn实现
1.基本实现
>>> from sklearn import svm
>>> X = [[0, 0], [1, 1]]
>>> y = [0, 1]
>>> clf = svm.SVC()
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
2.显示所得支持向量
>>> # get support vectors
>>> clf .support_vectors_ array([[ 0., 0.], [ 1., 1.]])
>>> # get indices of support vectors
>>> clf .support_
array([0, 1]...)
>>> # get number of support vectors for each class >>> clf .n_support_
array([1, 1]...)
3.多分类问题,
svm通过one-against-one策略,或ovr策略进行多分类,
具体如下:
If n_class
is the number of classes, then n_class * (n_class - 1) / 2
classifiers are constructed and each one trains data from two classes. To provide a consistent interface with other classifiers, the decision_function_shape
option allows to aggregate the results of the “one-against-one” classifiers to a decision function of shape (n_samples, n_classes)
:
也就是说可以通过decision_function_shape来确定多分类问题的求解方法
>>> X = [[ 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]])
>>> dec .shape[ 1] # 4 classes: 4*3/2 = 6 6
>>> clf .decision_function_shape = "ovr"
>>> dec = clf .decision_function([[ 1]])
>>> dec .shape[ 1] # 4 classes 4
4.SVM打分问题
原理上SVM算法不提供对于每一个分类的评价,需要通过其他算法来实现(sk-learn采用五折交叉验证)而这将会很耗时
通过方法:decision_function,参数probability等来实现
5.不平衡数据问题,通过调整对于不同类别数据的惩罚因子来改善
通过调整不同class的惩罚因子的相对大小,来改善该问题,经验上使得C*num(sample)相等
具体通过sk-learn
SVC
(but not NuSVC
) implement a keyword class_weight
in the fit
method. It’s a dictionary of the form {class_label : value}
, where value is a floating point number > 0 that sets the parameter C
of class class_label
to C * value
.
SVC
, NuSVC
, SVR
, NuSVR
and OneClassSVM
implement also weights for individual samples in method fit
through keyword sample_weight
. Similar to class_weight
, these set the parameter C
for the i-th example to C * sample_weight[i]
.
6。核函数参数
核函数·起到将低位空间的样本投影到高维空间的作用。sk-learn中常用的核函数包括
其参数通过所提示在SVC中定义的定义