1. 程式人生 > >(二)用 svm 識別手寫體數字圖片

(二)用 svm 識別手寫體數字圖片

一、解決問題

手寫體數字識別

二、程式碼分析

1、載入資料

from sklearn.datasets import load_digits
digits = load_digits()
digits.data.shape

2、資料分割

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(y_train.shape
) print(y_test.shape)

3、資料標準化

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)

4、建立模型預測資料

lsvc = LinearSVC()
lsvc.fit(X_train, y_train)
y_predict = lsvc.predict(X_test)

5、效能測試

from sklearn.metrics import classification_report
print(classification_report(y_test, y_predict, target_names = digits.target_names.astype(str)))