SVM
支持向量机
(Support Vector Machine, SVM)是一类按
监督学习
(supervised learning)方式对数据进行
二元分类
(binary classification)的广义线性分类器(generalized linear classifier),其
决策边界
是对学习样本求解的最大边距超平面(maximum-margin hyperplane)
**
流程
1. 收集数据:此处使用给定的文本文件
2. 准备数据:基于二值图像构造数据
3. 分析数据:对图像向量进行目测
4. 训练算法:采用三种不同的方法,不同的参数 线性分类器二次多项式核函数径向基核函数
5. 测试并计算错误率
以下直接使用sklearn.svm模块中的算法库对mnist数据集进行训练,并且进行预测:
import numpy as np
import struct
import matplotlib.pyplot as plt
import osimport pickle
##加载svm模型
from sklearn import svm
###用于做数据预处理
from sklearn import preprocessing
import time
##读取数据集
path='E:\\数据挖掘\\MNIST_data'
def load_mnist_train(path, kind='train'):
labels_path = os.path.join(path,'%s-labels.idx1-ubyte'% kind)
images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)
with open(labels_path, 'rb') as lbpath:
magic, n = struct.unpack('>II',lbpath.read(8))
labels = np.fromfile(lbpath,dtype=np.uint8)
with open(images_path, 'rb') as imgpath:
magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))
images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)
return images, labels
def load_mnist_test(path, kind='t10k'):
labels_path = os.path.join(path,'%s-labels.idx1-ubyte'% kind)
images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)
with open(labels_path, 'rb') as lbpath:
magic, n = struct.unpack('>II',lbpath.read(8))
labels = np.fromfile(lbpath,dtype=np.uint