课后题6.2、西瓜数据集3.0a上分别用线性核和高斯核训练一个SVM,并比较支持向量的区别。
使用LIBSVM经过训练之后发现,线性核和高斯核所得到的支持向量是一样的。LIBSVM的使用见这篇文章:LIBSVM使用
完整代码如下所示:
from libsvm.python.svmutil import *
import numpy as np
A = np.array([[0.697,0.460,1],[0.774,0.376,1],[0.634,0.264,1],[0.608,0.318,1],[0.556,0.215,1],
[0.403,0.237,1],[0.481,0.149,1],[0.437,0.211,1],[0.666,0.091,-1],[0.243,0.267,-1],
[0.245,0.057,-1],[0.343,0.099,-1],[0.639,0.161,-1],[0.657,0.198,-1],[0.360,0.370,-1],
[0.593,0.042,-1],[0.719,0.103,-1]])
y = A[:,2]
x = []
for i in range(len(y)):
dic = {}
dic[1] = A[i,0]
dic[2] = A[i,1]
x.append(dic)
lina_options = '-t 0 -c 1 -b 1' #线性核
guass_options = '-t 2 -c 4 -b 1' # 高斯核
model = svm_train(y,x,lina_options)
svm_save_model('./xiGua3.3alpha_linear',model)
model_ = svm_train(y,x,guass_options)
svm_save_model('./xiGua3.3alpha_Guass',model_)
得到的支持向量如下所示:
1 1:0.697 2:0.46
1 1:0.774 2:0.376
1 1:0.634 2:0.264
1 1:0.608 2:0.318
1 1:0.556 2:0.215
1 1:0.403 2:0.237
1 1:0.481 2:0.149
1 1:0.437 2:0.211
-1 1:0.666 2:0.091
-1 1:0.243 2:0.267
-1 1:0.343 2:0.099
-1 1:0.639 2:0.161
-1 1:0.657 2:0.198
-1 1:0.36 2:0.37
-1 1:0.593 2:0.042
-1 1:0.719 2:0.103
课后题6.3、用UCI数据集,分别用高斯核和线性核进行训练。
此处,选用了 iris数据集 中的前两类,分别标记为1和-1。用前40个样本进行训练,用后10个样本进行测试。
具体代码如下:
import numpy as np
from libsvm.python.svmutil import *
def readData(): # 读取数据
"""
Read data from txt file.
Return:
X1, y1, X2, y2, X3, y3: X is list with shape [50, 4],
y is list with shape [50,]
"""
X1 = []
y1 = []
X2 = []