支持向量机在高维或无限维空间中构造超平面或超平面集合,其可以用于分类、回归或其他任务。直观来说,分类边界距离最近的训练数据点越远越好,因为这样可以缩小分类器的泛化误差。
调用sklearn.svm的svc函数,将MNIST数据集进行分类,并将整体分类精度输出,这里用了两种预处理的方法(将特征值变成0或者1的数;将特征值变成0-1区间的数)效果不一样,并且分别调用了两种核函数(高斯核函数和多项式核函数)。在支持向量机实验中,将训练集和测试集都等分成10份,并求十份数据集整体分类精度的平均值,这样的结果较为准确客观。可以通过修改惩罚因子C的大小来看不同的效果,并画出图进行比较,C=100的时候效果较为好。
#任务:比较不同的kernel的结果差异,并画出相应的曲线来直观的表示
import struct
from numpy import *
import numpy as np
import time
from sklearn.svm import SVC#C-Support Vector Classification
def read_image(file_name):
#先用二进制方式把文件都读进来
file_handle=open(file_name,"rb") #以二进制打开文档
file_content=file_handle.read() #读取到缓冲区中
offset=0
head = struct.unpack_from('>IIII', file_content, offset) # 取前4个整数,返回一个元组
offset += struct.calcsize('>IIII')
imgNum = head[1] #图片数
rows = head[2] #宽度
cols = head[3] #高度
images=np.empty((imgNum , 784))#empty,是它所常见的数组内的所有元素均为空,没有实际意义,它是创建数组最快的方法
image_size=rows*cols#单个图片的大小
fmt='>' + str(image_size) + 'B'#单个图片的format
for i in range(imgNum):
images[i] = np.array(struct.unpack_from(fmt, file_content, offset))
# images[i] = np.array(struct.unpack_from(fmt, file_content, offset)).reshape((rows, cols))
offset += struct.calcsize(fmt)
return images
#读取标签
def read_label(file_name):
file_handle = open(file_name, "rb") # 以二进制打开文档
file_content = file_handle.read() # 读取到缓冲区中
head = struct.unpack_from('>II', file_content, 0) # 取前2个整数,返回一个元组
offset = struct.calcsize('>II')
labelNum = head[1] # label数
# print(labelNum)
bitsString = '>' + str(labelNum) + 'B' # fmt格式:'>47040000B'
label = struct.unpack_from(bitsString, file_content, offset) # 取data数据,返回一个元组
return np.array(label)
def normalize(data):#图片像素二值化,变成0-1分布
m=data.shape[0]
n=np.array(data).shape[1]
for i in range(m):
for j in range(n):
if data[i,j]!=0:
data[i,j]=1
else:
data[i,j]=0
return data
#另一种归一化的方法,就是将特征值变成[0,1]区间的数
def normalize_new(data):
m=data.shape[0]
n=np.array(data).shape[1]
for i in range(m):
for j in range(n):
data[i,j]=float(data[i,j])/255
return data
def loadDataSet():
train_x_filename="train-images-idx3-ubyte"
train_y_filename="train-labels-idx1-ubyte"
test_x_filename="t10k-images-idx3-ubyte"
test_y_filename="t10k-labels-idx1-ubyte"
train_x=read_image(train_x_filename)#60000*784 的矩阵
train_y=read_label(train_y_filename)#60000*1的矩阵
test_x=read_image(test_x_filename)#10000*784
test_y=read_label(test_y_filename)#10000*1
#可以比较这两种预处理的方式最后得到的结果
# train_x=normalize(train_x)
# test_x=normalize(test_x)
# train_x=normalize_new(train_x)
# test_x=normalize_new(test_x)
return train_x, test_x, train_y, test_y
if __name__=='__main__':
classNum=10
score_train=0.0
score=0.0
temp=0.0
temp_train=0.0
print("Start reading data...")
time1=time.time()
train_x, test_x, train_y, test_y=loadDataSet()
time2=time.time()
print("read data cost",time2-time1,"second")
print("Start training data...")
# clf=SVC(C=1.0,kernel='poly')#多项式核函数
clf = SVC(C=0.01,kernel='rbf')#高斯核函数
#由于每6000个中的每个类的数量都差不多相等,所以直接按照整批划分的方法
for i in range(classNum):
clf.fit(train_x[i*6000:(i+1)*6000,:],train_y[i*6000:(i+1)*6000])
temp=clf.score(test_x[i*1000:(i+1)*1000,:], test_y[i*1000:(i+1)*1000])
# print(temp)
temp_train=clf.score(train_x[i*6000:(i+1)*6000,:],train_y[i*6000:(i+1)*6000])
print(temp_train)
score+=(clf.score(test_x[i*1000:(i+1)*1000,:], test_y[i*1000:(i+1)*1000])/classNum)
score_train+=(temp_train/classNum)
time3 = time.time()
print("score:{:.6f}".format(score))
print("score:{:.6f}".format(score_train))
print("train data cost", time3 - time2, "second")
实验结果:对二值化(normalize)后的不同核函数和C的结果进行了统计和分析。结果如下表所示:
Parameter |
二值化 |
{ "C":1," " kernel": "poly"} |
{"accuarcy":0.4312,"train time":558.61} |
{"C":1, "kernel": "rbf"} |
{"accuarcy":0.9212,"train time":163.15} |
{"C":10, "kernel": "poly"} |
{"accuarcy":0.8802,"train time":277.78} |
{"C":10, "kernel": "rbf"} |
{"accuarcy":0.9354,"train time":96.07} |
{"C":100, "kernel": "poly"} |
{"accuarcy":0.9427,"train time":146.43} |
{"C":100, "kernel": "rbf"} |
{"accuarcy":0.9324,"train time":163.99} |
{"C":1000,"kernel":"poly"} |
{"accuarcy":0.9519,"train time":132.59} |
{"C":1000,"kernel":"rbf"} |
{"accuarcy":0.9325,"train time":97.54} |
{"C":10000,"kernel":"poly"} |
{"accuarcy":0.9518,"train time":115.35} |
{"C":10000,"kernel":"rbf"} |
{"accuarcy":0.9325,"train time":115.77} |
对于实验的优化方法,可以采用pca主成分分析方法,准确率和速度都有提升,代码如下:
结果截屏: