sklearn中的svm.SVC

svm是sklearn中一个关于支持向量机的包,比较常用,在使用过程中若是不熟悉各个参数的意义,总以默认参数进行机器学习,则不能做到最优化使用SVM,这就是一个较为遗憾的事情了。为了加深理解和方便调用,根据现有理解,结合官方文档,对其中的参数做一些记录,方便自己时常温习,也给阅读者进行一些粗浅的介绍,如果有理解错误的地方,希望阅读者能够指出。

以svm中的支持向量分类SVC作为介绍,所有参数如下:

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='ovr', 
            random_state=None)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

具体每个参数的使用方法介绍如下:

C : float, optional (default=1.0)

    误差项的惩罚参数,一般取值为10的n次幂,如10的-5次幂,10的-4次幂。。。。100次幂,101000,1000,在python中可以使用pow10,n) n=-5~inf
    C越大,相当于惩罚松弛变量,希望松弛变量接近0,即对误分类的惩罚增大,趋向于对训练集全分对的情况,这样会出现训练集测试时准确率很高,但泛化能力弱。
    C值小,对误分类的惩罚减小,容错能力增强,泛化能力较强。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
kernel : string, optional (default=’rbf’)

    svc中指定的kernel类型。
    可以是: ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ 或者自己指定。 默认使用‘rbf’ 。
  • 1
  • 2
  • 3
  • 4
  • 5
degree : int, optional (default=3)

    当指定kernel为 ‘poly’时,表示选择的多项式的最高次数,默认为三次多项式。
    若指定kernel不是‘poly’,则忽略,即该参数只对‘poly’有作用。
  • 1
  • 2
  • 3
  • 4
gamma : float, optional (default=’auto’)

    当kernel为‘rbf’, ‘poly’或‘sigmoid’时的kernel系数。
    如果不设置,默认为 ‘auto’ ,此时,kernel系数设置为:1/n_features
  • 1
  • 2
  • 3
  • 4
  • 5
coef0 : float, optional (default=0.0)

    kernel函数的常数项。
    只有在 kernel为‘poly’或‘sigmoid’时有效,默认为0
  • 1
  • 2
  • 3
  • 4
  • 5
probability : boolean, optional (default=False)
    是否采用概率估计。
    必须在fit()方法前使用,该方法的使用会降低运算速度,默认为False
  • 1
  • 2
  • 3
shrinking : boolean, optional (default=True)

    如果能预知哪些变量对应着支持向量,则只要在这些样本上训练就够了,其他样本可不予考虑,这不影响训练结果,但降低了问题的规模并有助于迅速求解。进一步,如果能预知哪些变量在边界上(即a=C),则这些变量可保持不动,只对其他变量进行优化,从而使问题的规模更小,训练时间大大降低。这就是Shrinking技术。

    Shrinking技术基于这样一个事实:支持向量只占训练样本的少部分,并且大多数支持向量的拉格朗日乘子等于C。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
tol : float, optional (default=1e-3)

    误差项达到指定值时则停止训练,默认为1e-3,即0.001
  • 1
  • 2
  • 3
  • 4
cache_size : float, optional

    指定内核缓存的大小,默认为200M。
  • 1
  • 2
  • 3
  • 4
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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
verbose : bool, default: False

    是否启用详细输出。
    多线程时可能不会如预期的那样工作。默认为False
  • 1
  • 2
  • 3
  • 4
max_iter : int, optional (default=-1)

    强制设置最大迭代次数。
    默认设置为-1,表示无穷大迭代次数。
    Hard limit on iterations within solver, or -1 for no limit.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
decision_function_shape : ‘ovo’, ‘ovr’, default=’ovr’

    ##这个用法也不是很理解##
    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).

    Changed in version 0.19: decision_function_shape is ‘ovr’ by default.

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

    Changed in version 0.17: Deprecated decision_function_shape=’ovo’ and None.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
random_state : int, RandomState instance or None, optional (default=None)

    伪随机数使用数据。 
  • 1
  • 2
  • 3


一些属性介绍:

Attributes: 

support_ : array-like, shape = [n_SV]

    Indices of support vectors.

support_vectors_ : array-like, shape = [n_SV, n_features]

    Support vectors.

n_support_ : array-like, dtype=int32, shape = [n_class]

    Number of support vectors for each class.

dual_coef_ : array, shape = [n_class-1, n_SV]

    Coefficients of the support vector in the decision function. For multiclass, coefficient for all 1-vs-1 classifiers. The layout of the coefficients in the multiclass case is somewhat non-trivial. See the section about multi-class classification in the SVM section of the User Guide for details.

coef_ : array, shape = [n_class-1, n_features]

    Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel.

    coef_ is a readonly property derived from dual_coef_ and support_vectors_.

intercept_ : array, shape = [n_class * (n_class-1) / 2]

    Constants in decision function.

猜你喜欢

转载自blog.csdn.net/CSDN_Gjx/article/details/78769702
今日推荐