Open In App

Datasets in Keras

Last Updated : 18 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Keras is a popular Python library used for building and training deep learning models. One common challenge in deep learning is accessing suitable datasets for model development. To address this, Keras includes a built-in module using:

keras.datasets

It provides several well-known datasets ready for use. Below are some widely used datasets which suitable for tasks like image classification, sentiment analysis and regression. These datasets are preprocessed and ready to use, making them ideal for training and evaluating deep learning models.

1. MNIST: Handwritten Digit Classification

The MNIST dataset is one of the most widely used benchmarks for image classification tasks. It consists of grayscale images of handwritten digits from 0 to 9. It includes:

  • Training Set: 60,000 images
  • Test Set: 10,000 images
  • Image Size: 28x28 pixels
python
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

Returns:

  • x_train, x_test: An unsigned integer(0-255) array of grayscale image data with shape (num_samples, 28, 28).
  • y_train, y_test: An unsigned integer(0-255) array of digit labels (integers in range 0-9) with shape (num_samples,).

2. Fashion-MNIST: Classification of Clothing Categories

Fashion-MNIST is a modern alternative to the original MNIST dataset and serves as a drop-in replacement for benchmarking image classification models. It contains grayscale images of various clothing items, making it more challenging and relevant for real-world applications. It has:

  • Training Set: 60,000 images
  • Test Set: 10,000 images
  • Image Size: 28x28 pixels
  • Number of Classes: 10 fashion categories
LabelDescription
0T-shirt/top
1Trouser
2Pullover
3Dress
4Coat
5Sandal
6Shirt
7Sneaker
8Bag
9Ankle boot
python
from keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

Returns:

  • x_train, x_test: An unsigned integer(0-255) array of grayscale image data with shape (num_samples, 28, 28).
  • y_train, y_test: An unsigned integer(0-255) array of digit labels (integers in range 0-9) with shape (num_samples,).

3. CIFAR-10

CIFAR-10 is a widely used dataset for benchmarking image classification models. It consists of color images grouped into 10 distinct categories making it suitable for evaluating performance on multi-class classification tasks involving real-world objects.

  • Training Set: 50,000 images (32x32 pixels, RGB)
  • Test Set: 10,000 images
  • Training Batches: 5 batches of 10,000 images each
  • Test Batch: 10,000 images, with 1,000 randomly selected from each class
LabelDescription
0Airplane
1Automobile
2Bird
3Cat
4Deer
5Dog
6Frog
7Horse
8Ship
9Truck

The dataset is well-balanced across classes. Each training class contains exactly 5,000 images, although the individual training batches vary slightly in class distribution.

python
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

Returns:

  • x_train, x_test: An unsigned integer(0-255) array of RGB image data with shape (num_samples, 3, 32, 32) or (num_samples, 32, 32, 3) based on the image_data_format backend setting of either channels_first or channels_last respectively. The value "3" in the shape refers to the 3 RGB channels.
  • y_train, y_test: An unsigned integer(0-255) array of category labels (integers in range 0-9) with shape (num_samples, 1).

4. CIFAR-100: Fine-Grained Image Classification

CIFAR-100 is an advanced version of the CIFAR-10 dataset, designed for more fine-grained image classification tasks. Like CIFAR-10, it contains 32x32 color images but with a total of 100 distinct classes each grouped into broader categories called superclasses.

  • Total Classes: 100 fine labels grouped into 20 superclasses (coarse labels)
  • Training Set: 50,000 images (500 per class)
  • Test Set: 10,000 images (100 per class)
  • Image Format: RGB, 32x32 pixels

Each image is annotated with:

  • A fine label representing the specific class like “maple tree”
  • A coarse label representing the superclass it belongs to like “trees”
python
from keras.datasets import cifar100
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')

Returns:

  • x_train, x_test: Arrays of RGB images with shape (num_samples, 32, 32, 3) for channels_last or (num_samples, 3, 32, 32) for channels_first (based on backend configuration).
  • y_train, y_test: Arrays of integer labels ranging from 0 to 99, with shape (num_samples, 1).

Arguments:

  • label_mode: "fine" or "coarse".

5. Boston Housing Prices: Regression on Real Estate Data

The Boston Housing dataset is a classic benchmark for regression tasks in machine learning. It was originally sourced from the StatLib library at Carnegie Mellon University and contains information collected in the 1970s about housing of Boston.

  • Problem Type: Regression
  • Features: 13 numerical attributes (e.g., crime rate, number of rooms, property tax rate)
  • Target: Median value of owner-occupied homes (in $1000s)
  • Training Set: 404 samples
  • Test Set: 102 samples

This dataset is useful for predicting continuous values based on multiple numerical features.

python
from keras.datasets import boston_housing
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()

Returns:

  • x_train, x_test: NumPy arrays of shape (num_samples, 13), representing the input features.
  • y_train, y_test: NumPy arrays of shape (num_samples,), representing the target house prices.

Arguments:

  • seed: Integer used to shuffle the data before splitting into training and test sets.
  • test_split: Float value representing the proportion of the dataset to allocate for testing.

6. IMDB Movie Reviews: Sentiment Classification

The IMDB dataset is a well-known benchmark for binary sentiment analysis, where the goal is to classify movie reviews as either positive or negative. It consists of 25,000 preprocessed reviews for training and 25,000 for testing each labeled with sentiment polarity.

  • Problem Type: Binary Classification
  • Data Format: Reviews are encoded as sequences of word indices where each index corresponds to a word ranked by frequency in the dataset. For example, the word mapped to index 5 is the 5th most common word.
  • Vocabulary Control: The dataset allows limiting the vocabulary size to include only the top N most frequent words, enabling efficient and compact modeling.
python
from keras.datasets import imdb
(x_train, y_train), (x_test, y_test) = imdb.load_data()

Returns:

  • x_train, x_test: Lists of sequences, where each sequence is a list of integers (word indices).
  • y_train, y_test: Lists of integer labels (1 = positive, 0 = negative).

Arguments:

  • num_words: Limit the vocabulary to the top num_words most frequent words. Less frequent words are replaced with oov_char.
  • skip_top: Skip the top N most frequent words, often stopwords.
  • maxlen: Maximum allowed length for sequences. Longer ones are truncated.
  • seed: For reproducible shuffling.
  • start_char: Token marking the beginning of a sequence (default is 1).
  • oov_char: Token used to replace words removed due to num_words or skip_top limits.
  • index_from: Shift word indices by a specified offset (useful when reserving special tokens like padding).

7. Reuters Newswire Topics: Multiclass Text Classification

The Reuters dataset is a classic benchmark for multiclass text classification. It contains 11,228 newswire articles collected by Reuters, each labeled with 46 distinct topics. This dataset is well-suited for evaluating models on text categorization tasks involving multiple classes.

  • Problem Type: Multiclass Classification
  • Number of Classes: 46 unique news topics
  • Data Format: Each article is preprocessed and represented as a sequence of integers where each integer corresponds to a word index based on frequency (similar to the IMDB dataset).
python
from keras.datasets import reuters
(x_train, y_train), (x_test, y_test) = reuters.load_data()

Returns:

  • x_train, x_test: Lists of sequences, where each sequence is a list of word indices (integers).
  • y_train, y_test: Lists of integer labels (ranging from 0 to 45), each representing a topic.

Arguments:

  • num_words: Limit vocabulary to the top num_words most frequent words.
  • skip_top: Skip the top N most frequent words to avoid overly common terms.
  • maxlen: Maximum allowed sequence length. Longer sequences will be truncated.
  • seed: Controls random shuffling for reproducibility.
  • start_char: Token marking the beginning of a sequence (default is 1).
  • oov_char: Token used to replace out-of-vocabulary words.
  • index_from: Index offset to start actual word indexing (useful when reserving special tokens).

Choosing the Right Dataset

Choosing the right dataset is important when experimenting with different architectures. The keras.datasets offers a variety of datasets suited to different machine learning tasks such as image classification, sentiment analysis and regression. The table below summarizes these datasets and their ideal use cases:

DatasetTask TypeDomainNumber of ClassesInput Shape
MNISTClassificationImage (grayscale)10(28, 28)
Fashion-MNISTClassificationImage (grayscale)10(28, 28)
CIFAR-10ClassificationImage (RGB)10(32, 32, 3)
CIFAR-100Fine-grained ClassificationImage (RGB)100(32, 32, 3)
IMDBSentiment Analysis (Binary)Text2Variable Length
ReutersTopic ClassificationText46Variable Length
Boston HousingRegressionTabular (numeric)(13,)

The keras.datasets module provides a set of standardized datasets for prototyping and benchmarking in deep learning. By offering preprocessed data across various domains, it helps developers focus on model building rather than data preparation.


Similar Reads