使用sklearn简单进行SVM参数优选

SVM简单回顾

支持向量机(SVM)方法建立在统计学VC维和结构风险最小化原则上,试图寻找到一个具有最大分类间隔的超平面,支持向量(Support Vector)是支持向量机训练的结果,在进行分类或者回归时也只依赖支持向量。因此SVM具有良好的鲁棒性,对未知数据拥有很强的泛化能力,特别是在数据量较少的情况下,相较其他传统机器学习算法具有更优的性能。
对于样本数据线性不可分的情况,通常采用核方法,将原始空间中的样本数据映射到一个更高维的特征空间,在该高维空间中样本数据线性可分。常用的核函数有线性核函数(Linear Kernel)、多项式核函数(Polynomial Kernel)、高斯核函数(RBF Kernel)和Sigmoid核函数(Sigmoid Kernel)。

名称 表达式 参数
线性核 k ( u , v ) = u T v + c
多项式核 k ( u , v ) = ( r u T v + c ) d c, d 1 为多项式次数
高斯核 k ( u , v ) = e x p ( | | u v | | 2 2 σ 2 ) σ > 0
Sigmoid核 k ( u , v ) = t a n h ( r u T v + c ) tanh为双曲正切函数

使用SVM作为模型时,通常采用如下流程:

  1. 对样本数据进行归一化
  2. 应用核函数对样本进行映射
  3. 用cross-validation和grid-search对超参数进行优选
  4. 用最优参数训练得到模型
  5. 测试

在第二步中最长采用和核函数是RBF核函数和线性核函数,在样本线性可分时,线性核函数效果要比RBF核函数好,有如下指导规则:

  1. 如果Feature的数量很大,甚至和样本数量差不多时,往往线性可分,这时选用LR或者线性核SVM;
  2. 如果Feature的数量很小,样本数量正常,不算多也不算少,这时选用RBF核SVM;
  3. 如果Feature的数量很小,而样本的数量很大,这时手动添加一些Feature,使得线性可分,然后选用LR或者线性核SVM。

Libsvm参数

Libsvm是一个简单的、容易使用的、高效的实现了SVM分类和回归的工具包。Libsvm可以从该链接获取
当使用svm-train时,主要涉及以下参数:
-s svm_type : set type of SVM (default 0)
0 – C-SVC (multi-class classification)
1 – nu-SVC (multi-class classification)
2 – one-class SVM
3 – epsilon-SVR (regression)
4 – nu-SVR (regression)

-t kernel_type : set type of kernel function (default 2)
0 – linear: u’*v
1 – polynomial: (gamma*u’*v + coef0)^degree
2 – radial basis function: exp(-gamma*|u-v|^2)
3 – sigmoid: tanh(gamma*u’*v + coef0)

-d degree : set degree in kernel function (default 3),设置多项式核的degree值
-g gamma : set gamma in kernel function (default 1/num_features),多项式/RBF/Sigmoid核的gamma值
-r coef0 : set coef0 in kernel function (default 0),设置多项式/Sigmoid核的coef0值
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1),设置惩罚函数的力度
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5),设置nu-SVC/one-class SVM/nu-SVR的nu值
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1),设置epsilon-SVR的损失函数中epsilon值
-m cachesize : set cache memory size in MB (default 100),设置cache内存的大小,单位MB
-e epsilon : set tolerance of termination criterion (default 0.001),设置终止的判据
-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1),是否允许使用启发式
-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0),是否进行概率估计
-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1),设置C-SVC中第i类的参数C为weight*C
-v n: n-fold cross validation mode,设置交叉检验
-q : quiet mode (no outputs)

对于RBF核函数,其中比较重要的参数有c,g。其中c表示对误差的惩罚程度,c越大,表示对误差的惩罚程度越大,模型对样本数据的学习更精确,也因此容易过拟合;反之,c越小,对误差的惩罚程度越小,可能欠拟合。g对应RBF核中的gamma值,gamma越大, σ 越小,使得高斯分布又高又瘦,造成模型只能作用于支持向量附近,也就是过拟合;反之,gamma越小, σ 越大,高斯分布会过于平滑,在训练集上分类效果不佳,也就是欠拟合。
最佳c,g组合的确定可以采用最简单的网格搜索方法,sklearn中提供了相应的方法GridSearchCV:

class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise', return_train_score=True)

比较重要的参数有:
estimator:使用的模型
param_grid:参数列表,如下展示了分别使用RBF核函数与线性核函数时参数选择

parameters = [
    {
        'C': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
        'gamma': [0.00001, 0.0001, 0.001, 0.1, 1, 10, 100, 1000],
        'kernel': ['rbf']
    },
    {
        'C': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
        'kernel': ['linear']
    }
]

scoring:评价指标,默认采用estimator的score函数
cv:交叉验证参数,默认为3,使用3折交叉验证
n_jobs:并行数

之后调用GridSearchCV对象的fit()方法在训练数据集上运行网格搜索,选出最优参数,然后在测试集上测试模型。GridSearchCV对象还包括如下常用属性:
grid.scores:给出不同参数组合下的评价结果
grid.best_params_:最佳结果的参数组合
grid.best_score_:最佳评价得分
grid.best_estimator_:最佳模型
完整的例子如下:

from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV

svc = SVC()
parameters = [
    {
        'C': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
        'gamma': [0.00001, 0.0001, 0.001, 0.1, 1, 10, 100, 1000],
        'kernel': ['rbf']
    },
    {
        'C': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
        'kernel': ['linear']
    }
]
clf = GridSearchCV(svc, parameters, cv=5, n_jobs=8)
clf.fit(train_data, train_data_tag)
print(clf.best_params_)
best_model = clf.best_estimator_
best_model.predict(test_data)

当然随着数据量的增大,GridSearchCV的效率将变得非常低,可以采用一些其他更加高级的调优方法。

猜你喜欢

转载自blog.csdn.net/hohaizx/article/details/79656496
今日推荐