Loading image data from the MNIST dataset in Keras is a straightforward process. You can use the keras.datasets
module to load the MNIST dataset, which includes 60,000 training images and 10,000 testing images of handwritten digits. Here is an example code to load the MNIST dataset in Keras:
pythonfrom keras.datasets import mnist
# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Print the shape of the training and testing data
print("Training Data Shape:", X_train.shape) # (60000, 28, 28)
print("Testing Data Shape:", X_test.shape) # (10000, 28, 28)
In the code above, X_train
and y_train
are the training data and labels, respectively, while X_test
and y_test
are the testing data and labels, respectively. The load_data()
function returns the data in the form of NumPy arrays.
After loading the data, you may need to preprocess it before feeding it into a Keras model. For example, you may need to normalize the pixel values to have a value between 0 and 1. You can do this by dividing the pixel values by 255, which is the maximum value a pixel can have. Here is an example of how to normalize the data:
python# Normalize the data
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0
In this code, we first convert the data type of X_train
and X_test
to float32
. Then, we divide the pixel values by 255.0 to normalize them.
Once you have loaded and preprocessed the data, you can use it to train a Keras model.
Comments
Post a Comment