Scikit-learn is a powerful library in Python that provides simple and efficient tools for data analysis and machine learning. One of its exceptionally useful features is the ability to load various datasets using built-in data loader functions. In this article, we will explore the load_digits function to execute a simple digit recognition task.
Exploring the Digits Dataset
The `load_digits` function is part of the datasets module in Scikit-learn. The digits dataset contains images of handwritten digits. Let's start by importing the necessary libraries and loading this dataset.
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt
# Load digits dataset
digits = load_digits()
# Printing the shape of the data
print("Shape of image data:", digits.data.shape)
Each image in the dataset is of size 8x8 pixels, flattened into a 64-length feature vector, providing 1797 samples of handwritten digits from 0 to 9.
Visualizing the Data
Let's visualize a few samples from this dataset to better understand what our model will be working with. Using Matplotlib, we can plot some of these digits:
fig, axes = plt.subplots(2, 5, figsize=(10, 5))
for ax, image, label in zip(axes.ravel(), digits.images, digits.target):
ax.set_axis_off()
ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
ax.set_title('Label: %d' % label)
plt.show()This will display the first ten images in our dataset along with their respective labels.
Preparing the Data
To train a model, it is common to split the data into training and testing sets. We can achieve this using Scikit-learn's train_test_split function:
from sklearn.model_selection import train_test_split
# Splitting dataset into training (75%) and testing (25%)
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=0)
Training a Model
For this guide, we’ll use a Support Vector Machine (SVM), a robust and powerful model for classification tasks provided by Scikit-learn. The model will be trained on the training data and evaluated on the test data:
from sklearn import svm
from sklearn.metrics import accuracy_score
# Create an SVM classifier
classifier = svm.SVC(gamma=0.001)
# Fit classifier to training data
classifier.fit(X_train, y_train)
Evaluating the Model
After the model is trained, evaluate its accuracy by comparing the predicted values against the actual targets:
# Predicting the test set results
predicted = classifier.predict(X_test)
# Calculating accuracy
accuracy = accuracy_score(y_test, predicted)
print("Accuracy:", accuracy)With this, you will get the accuracy of the model on the test set which should be fairly high for a simple dataset like digits.
Conclusion
The Scikit-learn digits dataset is a great place to start for anyone interested in learning about machine learning techniques for image classification. By leveraging Scikit-learn's built-in datasets and tools, you can quickly apply algorithms, evaluate their performance, and gain insights into the challenges of digit recognition tasks. Experiment with other classifiers or modify hyperparameters for the SVM to see how your results vary!