1.手写体数据读取代码样例
# 1.load hand write datasets
from sklearn.datasets import load_digits
digits = load_digits()
print digits.data.shape
输出:
(1797, 64)
2.手写体数据分割代码样例
# 2.data split to train and test data
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
输出:
(1347,)
(450,)
3.使用支持向量机(分类)对手写体数字图像进行识别
# 3.to predict
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 LinearSVC is ', 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))
输出:
The Accuracy of LinearSVC is 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