使用sklearn自带的手写体数据集,利用SVM模型进行手写体识别
#-*-coding:utf-8-*-
#导入手写体数字加载器
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
#从sklearn.preprocessing里导人数据标准化模块
from sklearn.preprocessing import StandardScaler
#从sklearn.svm里导人基于线性假设的支持向量机分类器LinearSVC.
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
digits=load_digits()
digits.data.shape
#数据分割
X_train,X_test,y_train,y_test=train_test_split(digits.data,digits.target,test_size=0.25,random_state=33)
# y_train.shape
# y_test.shape
#数据标准化
ss=StandardScaler()
x_train=ss.fit_transform(X_train)
x_test = ss.fit_transform(X_test)
lsvc=LinearSVC
lsvc.fit(x_train,y_train)
y_predict=lsvc.predict(x_test)
#支持向量机(分类)模型对手写体数码图像识别能力评估
print('The Accuracy of Linear SVC is',lsvc.score(x_test,y_test))
print(classification_report(y_test,y_predict,target_names=digits.target_names.astype(str)))
作者:WangB