In [1]:
# Chapter 3
# Classifying images
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
In [2]:
# Load the dataset
digits = load_digits()
In [3]:
# Showing shapes
print("Image Data Shape {}".format(digits.data.shape))
In [4]:
print("Label Data Shape {}".format(digits.target.shape))
In [5]:
digits.data[:3]
Out[5]:
In [6]:
# Let's represent the images
plt.figure(figsize=(20,4))
for index, (image, label) in enumerate(zip(digits.data[0:5], digits.target[0:5])):
plt.subplot(1, 5, index + 1)
plt.imshow(np.reshape(image, (8,8)), cmap=plt.cm.gray)
plt.title("Training: {}\n".format(label), fontsize = 20)
In [7]:
# Split training / testing datasets
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=0)
In [8]:
# Logistic regression
# Training
lreg_model = LogisticRegression()
lreg_model.fit(X_train, y_train)
Out[8]:
In [9]:
# Calculate predictions
y_pred = lreg_model.predict(X_test)
In [10]:
# Show model accuracy
score = lreg_model.score(X_test, y_test)
print("Logistic regression model score: {:.2f}".format(score))
In [11]:
# Now we'll do it using SVM
from sklearn.svm import SVC
In [13]:
# Using a linear kernel
svc_linear_kernel = SVC(kernel='linear')
svc_linear_kernel.fit(X_train, y_train)
print("SVC with linear kernel score ---> {:.2f}".format(svc_linear_kernel.score(X_test, y_test)))
In [14]:
# Using a polynomial kernel
svc_poly_kernel = SVC(kernel='poly')
svc_poly_kernel.fit(X_train, y_train)
print("SVC with polynomial kernel score ---> {:.2f}".format(svc_poly_kernel.score(X_test, y_test)))
In [ ]: