0. 区别
关于sklearn.svm.SVC与.NuSVC的区别以及参数介绍,SVC与NuSVC是类似的方法,但是接受稍微不同的参数集合并具有不同的数学公式 ,并且NuSVC可以使用参数来控制支持向量的个数 , 以下代码默认的是多分类。
1. SVC
# coding:utf-8
from sklearn import svm
from numpy import *
X = array([[0], [1], [2], [3]])
y = array([0, 1, 2, 3])
clf = svm.SVC()
clf.fit(X, y)
print("predict :",clf.predict([4]))
print(clf)
print("support index :", clf.support_)
print("support Vec :", clf.support_vectors_,'\n','--'*5)
print("number in class :", clf.n_support_)
print("class label :",clf.classes_)
print("len of classes :",len(clf.intercept_)) # 多分类器长度,1v1投票,见后文
# 打印结果
'''
predict : [3]
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)
support index : [0 1 2 3]
support Vec : [[ 0.]
[ 1.]
[ 2.]
[ 3.]]
----------
number in class : [1 1 1 1]
class label : [0 1 2 3]
len of classes : 6
'''
# 注释/解释
'''
关于参数
C: 目标函数的惩罚系数C,C越大,对训练集测试时准确率越高,但泛化能力越弱,C值小,对误分类的惩罚减小,允许容错,泛化能力越强...默认1.0
shrinking: 启发式,还没解决,有助于计算效果...
verbose: 展现细节...就是允许冗余输出
coef0: 核函数的常数项,对于poly和sigmoid核函数有用
degree: 多项式poly核函数的维度,默认是3,其他核函数会忽略
gamma: 不清楚,但调参后效果明显;rbf,poly和sigmoid的核函数参数
tol: 停止训练的误差值大小,默认为1e-3
调参数 - 目前调整C与gamma效果不错(针对kaggle的训练)
'''
百度 : 启发式
vc”>2. NuSVC
# coding:utf-8
from sklearn import svm
from numpy import *
X = array([[0], [1], [2], [3]])
y = array([0, 1, 2, 3])
clf = svm.NuSVC()
clf.fit(X,y)
print("predict :",clf.predict([4]))
print(clf)
print("support index :", clf.support_)
print("support Vec :", clf.support_vectors_,'\n','--'*5)
print("number in class :", clf.n_support_)
print("class label :",clf.classes_)
print("len of classes :",len(clf.intercept_))
# 打印结果
'''
predict : [3]
NuSVC(cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, nu=0.5, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False)
support index : [0 1 2 3]
support Vec : [[ 0.]
[ 1.]
[ 2.]
[ 3.]]
----------
number in class : [1 1 1 1]
class label : [0 1 2 3]
len of classes : 6
'''
# 注释解释
'''
不同的参数:
nu: 官方解释 An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1].
调参有效果,可参考
'''
3. 多分类投票
可以参考一下官方文档
SVM算法最初是为二值分类问题设计的,当处理多类问题时,就需要构造合适的多类分类器,主要有1v1,1vN
1. 1v1 一对一法 : 两两为一类,一共 C2n 类,进行投票,Libsvm中的多类分类就是根据这个方法实现的。
2. 1vN 一对多法 : 训练时依次把某个类别的样本归为一类,其余的就是其他类,这样就有N类,样本选择最大分类函数值的那一类。