使用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