sklearn中SVM调参说明
寫在前面
之前只停留在理論上,沒有實(shí)際沉下心去調(diào)參,實(shí)際去做了后,發(fā)現(xiàn)調(diào)參是個大工程(玄學(xué))。于是這篇來總結(jié)一下sklearn中svm的參數(shù)說明以及調(diào)參經(jīng)驗(yàn)。方便以后查詢和回憶。
常用核函數(shù)
1.linear核函數(shù):
K(xi,xj)=xTixjK(xi,xj)=xiTxj
2.polynomial核函數(shù):
3.RBF核函數(shù)(高斯核函數(shù)):
4.sigmoid核函數(shù):
?
sklearn svm 相關(guān)參數(shù)的官方說明
Parameters:
C?: float, optional (default=1.0). Penalty parameter C of the error term.
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.
gamma?: float, optional (default=’auto’). Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’. If gamma is ‘a(chǎn)uto’ 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.
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_classesnp.bincount(y))
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.
libsvm中參數(shù)說明
因?yàn)閟klearn底層是調(diào)用libsvm的,因此sklearn中svm參數(shù)說明是可以直接參考libsvm中的。
1.linear核函數(shù):
K(xi,xj)=xTixjK(xi,xj)=xiTxj
2.polynomial核函數(shù):
3.RBF核函數(shù)(高斯核函數(shù)):
4.sigmoid核函數(shù):
?
首先介紹下與核函數(shù)相對應(yīng)的參數(shù):
1)對于線性核函數(shù),沒有專門需要設(shè)置的參數(shù)
2)對于多項式核函數(shù),有三個參數(shù)。-d用來設(shè)置多項式核函數(shù)的最高次項次數(shù),也就是公式中的d,默認(rèn)值是3。-g用來設(shè)置核函數(shù)中的gamma參數(shù)設(shè)置,也就是公式中的gamma,默認(rèn)值是1/k(特征數(shù))。-r用來設(shè)置核函數(shù)中的coef0,也就是公式中的第二個r,默認(rèn)值是0。
3)對于RBF核函數(shù),有一個參數(shù)。-g用來設(shè)置核函數(shù)中的gamma參數(shù)設(shè)置,也就是公式中g(shù)amma,默認(rèn)值是1/k(k是特征數(shù))。
4)對于sigmoid核函數(shù),有兩個參數(shù)。-g用來設(shè)置核函數(shù)中的gamma參數(shù)設(shè)置,也就是公式中g(shù)amma,默認(rèn)值是1/k(k是特征數(shù))。-r用來設(shè)置核函數(shù)中的coef0,也就是公式中的第二個r,默認(rèn)值是0。
具體來說說rbf核函數(shù)中C和gamma?:
SVM模型有兩個非常重要的參數(shù)C與gamma。其中 C是懲罰系數(shù),即對誤差的寬容度。c越高,說明越不能容忍出現(xiàn)誤差,容易過擬合。C越小,容易欠擬合。C過大或過小,泛化能力變差
gamma是選擇RBF函數(shù)作為kernel后,該函數(shù)自帶的一個參數(shù)。隱含地決定了數(shù)據(jù)映射到新的特征空間后的分布,gamma越大,支持向量越少,gamma值越小,支持向量越多。支持向量的個數(shù)影響訓(xùn)練與預(yù)測的速度。
這里面大家需要注意的就是gamma的物理意義,大家提到很多的RBF的幅寬,它會影響每個支持向量對應(yīng)的高斯的作用范圍,從而影響泛化性能。我的理解:如果gamma設(shè)的太大,方差會很小,方差很小的高斯分布長得又高又瘦, 會造成只會作用于支持向量樣本附近,對于未知樣本分類效果很差,存在訓(xùn)練準(zhǔn)確率可以很高,(如果讓方差無窮小,則理論上,高斯核的SVM可以擬合任何非線性數(shù)據(jù),但容易過擬合)而測試準(zhǔn)確率不高的可能,就是通常說的過訓(xùn)練;而如果設(shè)的過小,則會造成平滑效應(yīng)太大,無法在訓(xùn)練集上得到特別高的準(zhǔn)確率,也會影響測試集的準(zhǔn)確率。
此外,可以明確的兩個結(jié)論是:
結(jié)論1:樣本數(shù)目少于特征維度并不一定會導(dǎo)致過擬合,這可以參考余凱老師的這句評論:
“這不是原因啊,呵呵。用RBF kernel, 系統(tǒng)的dimension實(shí)際上不超過樣本數(shù),與特征維數(shù)沒有一個trivial的關(guān)系。”
結(jié)論2:RBF核應(yīng)該可以得到與線性核相近的效果(按照理論,RBF核可以模擬線性核),可能好于線性核,也可能差于,但是,不應(yīng)該相差太多。
當(dāng)然,很多問題中,比如維度過高,或者樣本海量的情況下,大家更傾向于用線性核,因?yàn)樾Ч喈?dāng),但是在速度和模型大小方面,線性核會有更好的表現(xiàn)。
Reference
http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC
http://blog.csdn.net/lqhbupt/article/details/8610443
http://blog.csdn.net/lujiandong1/article/details/46386201
轉(zhuǎn)載于:https://www.cnblogs.com/sddai/p/9696771.html
總結(jié)
以上是生活随笔為你收集整理的sklearn中SVM调参说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpaceX:欢迎“攻击”星链 最高奖励
- 下一篇: 循环分支循环语句