How to train an image classification model using scikit-learn on the MNIST dataset of handwritten digits
Published on Aug. 22, 2023, 12:16 p.m.
To train an image classification model using scikit-learn on the MNIST dataset of handwritten digits, you can use the sklearn.datasets.load_digits
function to load the data and then train a classifier using the extracted features. Here is an example code snippet:
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from joblib import dump, load
# Load the data
digits = load_digits()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.3, random_state=42)
# Train the model
classifier = RandomForestClassifier(n_estimators=100)
classifier.fit(X_train, y_train)
# Test the model
y_pred = classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
# Save the model
dump(classifier, 'mnist_classifier.joblib')
# Load the model
loaded_classifier = load('mnist_classifier.joblib')
# Predict the output for a new input
test_input = X_test[0]
predicted_label = loaded_classifier.predict([test_input])
print(predicted_label)
In this code, we first load the MNIST data using the load_digits
function from the sklearn.datasets
module, and split it into training and testing sets using the train_test_split
function.
We then train a random forest classifier on the training set using the RandomForestClassifier
class, and test its accuracy on the testing set using the accuracy_score
function from the sklearn.metrics
module.
Next, we save the trained model to disk using the dump()
method from the joblib
module.
Finally, we can load the saved model using the load()
method from the joblib
module, and use the trained model to make predictions for new inputs by calling the predict()
method on the loaded classifier.
This is just a basic example, but there are many other options in scikit-learn for training and evaluating image classification models.
MLPClassifier
To train a handwritten digit classification model using the multilayer perceptron (MLP) algorithm in scikit-learn, you can use the MLPClassifier
class, which allows you to specify the architecture of the neural network, such as the number of layers, the number of neurons per layer, and the activation function used. Here’s an example code snippet:
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, classification_report
from joblib import dump, load
# Load the data
digits = load_digits()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.3, random_state=42)
# Train the model
clf = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=0.0001, solver='sgd',
verbose=10, random_state=1, learning_rate_init=.1)
clf.fit(X_train, y_train)
# Test the model
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
print('Classification Report:', classification_report(y_test, y_pred))
# Save the model
dump(clf, 'mnist_mlp_classifier.joblib')
# Load the model
loaded_clf = load('mnist_mlp_classifier.joblib')
# Predict the output for a new input
test_input = X_test[0]
predicted_label = loaded_clf.predict([test_input])
print(predicted_label)
In this code, we first load the MNIST data using the load_digits
function from the sklearn.datasets
module, and split it into training and testing sets using the train_test_split
function.
We then train an MLP classifier on the training set using the MLPClassifier
class, specifying the number of neurons in a single hidden layer to be 50, the maximum number of iterations to be 10, the learning rate to be 0.1, and the backpropagation solver to be stochastic gradient descent (SGD).
Next, we test the accuracy of the trained model on the testing set using the accuracy_score
function from the sklearn.metrics
module, and print out a classification report using the classification_report
function.
We then save the trained model to disk using the `