本文所有实现代码均来自《Python机器学习及实战》
#-*- coding:UTF-8 -*-
#第一步:手写体数据读取
#从sklearn.datasets中导入手写体数字加载器
from sklearn.datasets import load_digits
#从通过数据加载器获得的手写体数字的数码图像数据并储存在digits中
digits = load_digits()
#查看数据规模和特征维度
print digits.data.shape
#输出结果为(1797,64),表明数字图像数据共有1797条,并且每幅图片是8*8=64的像素矩阵表示
#在模型使用这些像素矩阵时,通常将2D的图片转成首尾相接的1D的像素特征向量表示,即 8*8 -> 64*1,即此处的特征维度为64
#大部分经典模型都没有对结构化数据进行学习的能力,不过深度学习中,有一些对结构化数据学习的技术
#第二步:对于没有直接提供测试样本的数据,只能自己将训练数据进行分割
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(digits.data,digits.target,test_size=0.25,random_state=33)
#查看训练数据和测试数据的规模
print X_train.shape,X_test.shape
#第三步:基于线性假设的SVM模型来训练数据
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
ss=StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)
lsvc = LinearSVC()
lsvc.fit(X_train,y_train)
y_predict = lsvc.predict(X_test)
#第四步:SVM对手写体数码图像识别能力的评估
print 'Accuracy Of Lsvc:',lsvc.score(X_test,y_test)
from sklearn.metrics import classification_report
print classification_report(y_test,y_predict,target_names=digits.target_names.astype(str))
#总结
#根据svm的精妙模型假设,可以帮助我们在海量甚至高维度的数据中,筛选出对预测任务最有效的少数训练样本
#既节省了模型学习所需要的数据内存,同时也提高了模型的预测性能(不用考虑所有数据,避免了过拟合)
#耗费的代价是需要更多的CPU资源和计算时间
#******************************************************************************************
#[补充]接下来使用不同核函数配置下的SVC进行对比
#(1)配置为kernel='linear'的SVC
from sklearn.svm import SVC
linear_svc = SVC(kernel='linear')
linear_svc.fit(X_train,y_train)
y_linear_svc_predict = linear_svc.predict(X_test)
#评估模型性能
print 'Accuracy Of linear_svc:',linear_svc.score(X_test,y_test)
from sklearn.metrics import classification_report
print classification_report(y_test,y_linear_svc_predict,target_names=digits.target_names.astype(str))
#(2)配置为kernel='poly'的SVC
from sklearn.svm import SVC
poly_svc = SVC(kernel='poly')
poly_svc.fit(X_train,y_train)
y_poly_svc_predict = poly_svc.predict(X_test)
#评估模型性能
print 'Accuracy Of poly_svc:',poly_svc.score(X_test,y_test)
from sklearn.metrics import classification_report
print classification_report(y_test,y_poly_svc_predict,target_names=digits.target_names.astype(str))
#(3)配置为kernel='rbf'的SVC
from sklearn.svm import SVC
rbf_svc = SVC(kernel='rbf')
rbf_svc.fit(X_train,y_train)
y_rbf_svc_predict = rbf_svc.predict(X_test)
#评估模型性能
print 'Accuracy Of rbf_svc:',rbf_svc.score(X_test,y_test)
from sklearn.metrics import classification_report
print classification_report(y_test,y_rbf_svc_predict,target_names=digits.target_names.astype(str))
1.使用LinearSVC模型的输出结果:
Accuracy Of Lsvc: 0.953333333333
precision recall f1-score support
0 0.92 1.00 0.96 35
1 0.96 0.98 0.97 54
2 0.98 1.00 0.99 44
3 0.93 0.93 0.93 46
4 0.97 1.00 0.99 35
5 0.94 0.94 0.94 48
6 0.96 0.98 0.97 51
7 0.92 1.00 0.96 35
8 0.98 0.84 0.91 58
9 0.95 0.91 0.93 44
avg / total 0.95 0.95 0.95 450
2.使用SVC(kernel=‘linear’)模型的输出结果:
Accuracy Of linear_svc: 0.975555555556
precision recall f1-score support
0 1.00 1.00 1.00 35
1 0.93 1.00 0.96 54
2 1.00 0.98 0.99 44
3 0.96 1.00 0.98 46
4 0.97 0.97 0.97 35
5 0.98 0.98 0.98 48
6 1.00 0.98 0.99 51
7 1.00 1.00 1.00 35
8 0.98 0.90 0.94 58
9 0.96 0.98 0.97 44
avg / total 0.98 0.98 0.98 450
3.使用SVC(kernel=‘poly’)模型的输出结果:
Accuracy Of poly_svc: 0.955555555556
precision recall f1-score support
0 1.00 1.00 1.00 35
1 1.00 0.98 0.99 54
2 1.00 1.00 1.00 44
3 1.00 0.87 0.93 46
4 1.00 0.94 0.97 35
5 0.98 0.96 0.97 48
6 1.00 0.98 0.99 51
7 1.00 1.00 1.00 35
8 0.75 1.00 0.86 58
9 1.00 0.82 0.90 44
avg / total 0.97 0.96 0.96 450
4.使用SVC(kernel=‘rbf’)模型的输出结果:
Accuracy Of rbf_svc: 0.986666666667
precision recall f1-score support
0 1.00 1.00 1.00 35
1 0.98 1.00 0.99 54
2 1.00 0.98 0.99 44
3 1.00 1.00 1.00 46
4 0.97 0.94 0.96 35
5 0.98 0.98 0.98 48
6 0.98 1.00 0.99 51
7 1.00 1.00 1.00 35
8 0.98 0.98 0.98 58
9 0.98 0.98 0.98 44
avg / total 0.99 0.99 0.99 450