# import all packages we need.
import numpy as np
import matplotlib.pyplot as plt
# import dataset and packages from sklearn
from sklearn import datasets
from sklearn.preprocessing import scale, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import svm, metrics
# load dataset for
digits = datasets.load_digits()
digits_target = digits.target
digits_images = digits.images
number_digits = len(np.unique(digits.target))
print(digits.keys())
print(digits.target)
print(digits.data.shape)
print(number_digits)
print(digits_target.shape)
print(digits_images.shape)
dict_keys(['data', 'target', 'frame', 'feature_names', 'target_names', 'images', 'DESCR']) [0 1 2 ... 8 9 8] (1797, 64) 10 (1797,) (1797, 8, 8)
# 設定圖形的大小(寬, 高)
fig = plt.figure(figsize =(8,4))
# 調整子圖形
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(12):
ax = fig.add_subplot(2,6, i+1, xticks=[], yticks=[])
ax.imshow(digits_images[i], cmap = plt.cm.binary)
ax.text(0,7,str(digits_target[i]),color='red', fontsize=14)
plt.show()
# normalize dataset
data = scale(digits.data)
# split dataset into training (75%) and testing (25%)
X_train, X_test, y_train, y_test, image_train, image_test = train_test_split(data, digits.target, digits.images, test_size = 0.25, random_state = 42)
print(X_train.shape, X_test.shape)
(1347, 64) (450, 64)
# Create the SVM model
svc_model = svm.SVC(C=20, kernel='linear')
# train the SVM model with training dataset.
svc_model.fit(X_train, y_train)
# use the trained model for testing dataset.
predicted = svc_model.predict(X_test)
disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted,cmap='CMRmap_r')
#print(metrics.classification_report(y_test, predicted))
print(metrics.confusion_matrix(y_test, predicted))
print('Accuracy :',metrics.accuracy_score(y_test, predicted))
[[43 0 0 0 0 0 0 0 0 0] [ 0 37 0 0 0 0 0 0 0 0] [ 0 0 38 0 0 0 0 0 0 0] [ 0 0 1 44 0 1 0 0 0 0] [ 0 0 0 0 55 0 0 0 0 0] [ 0 0 0 1 0 57 1 0 0 0] [ 1 0 0 0 0 0 44 0 0 0] [ 0 0 0 0 0 0 0 40 0 1] [ 0 1 0 0 0 1 0 0 36 0] [ 0 0 0 1 0 0 0 1 0 46]] Accuracy : 0.9777777777777777
# check some results
fig = plt.figure(figsize =(8,4))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(12):
ax = fig.add_subplot(2,6, i+1, xticks=[], yticks=[])
ax.imshow(X_test[i,:].reshape((8,8)), cmap = plt.cm.binary)
ax.text(0,7,str(predicted[i]),color='red', fontsize=14)
plt.show()