? ? ? ? sklearn內(nèi)部集成了一些手寫體數(shù)字圖片數(shù)據(jù)集,現(xiàn)在我們使用這些數(shù)據(jù),用SVM支持向量機(jī)算法進(jìn)行訓(xùn)練識別的練習(xí)。筆者習(xí)慣用pycharm,今天手癢,用一下Spyder編輯,順便對比一下哪一個(gè)好用。廢話不多說,上碼:
#-*- coding:utf-8 -*-
#------導(dǎo)入sklearn里的數(shù)據(jù)集
from sklearn.datasets import load_digits
digits=load_digits()
print('數(shù)據(jù)集的數(shù)據(jù)量及單個(gè)數(shù)據(jù)的大?。?,digits.data.shape)
#------訓(xùn)練集和測試集隨機(jī)分割,25%作為測試集
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ùn)練集數(shù)量:',y_train.shape)
print('測試集數(shù)量:',y_test.shape)
#------調(diào)用SVM算法訓(xùn)練模型
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)
#-------性能評估
print('The accuracy of Linear SVCis:',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)))
? ? ?結(jié)果如下:
準(zhǔn)確率不錯(cuò)的,識別手寫體數(shù)字準(zhǔn)確在95%以上。