digits: Handwritten digit images

digitsR Documentation

Handwritten digit images

Description

A subset of MNIST containing 3,000 handwritten examples of each of the digits 0, 1, 2 and 3. Each observation consists of a class label and a flattened 28 by 28 grayscale image.

Usage

data("digits")

Format

An integer matrix with 12,000 rows and 785 columns. The first column, label, contains the digit class (0, 1, 2 or 3). The remaining 784 columns, named pixel0 through pixel783, contain grayscale pixel values from 0 to 255. There are 3,000 rows per class.

Details

Use only the pixel columns when fitting a decomposition or another unsupervised method. Keeping class labels separate allows the fitted representation to be compared with the known digits without treating the labels as image features. The stored intensities are not centered, standardized or divided by 255. Some background pixels may be constant across a chosen subset, so scaling every pixel to unit variance can be inappropriate.

The example reshapes the 784 pixel values from one observation into a 28 by 28 matrix. Reversing its second axis gives the display orientation used by image. The reversed grayscale palette shows dark handwriting on a light background.

References

Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner (1998). Gradient-based learning applied to document recognition. Proceedings of the IEEE, 86(11), 2278–2324.

Examples

data("digits")
dim(digits)
table(digits[, "label"])

# Keep the label separate from the pixels.
label <- digits[1L, "label"]
digit <- matrix(digits[1L, -1L], nrow = 28, ncol = 28)
image(digit[, 28:1], col = gray((255:0) / 255), asp = 1,
      axes = FALSE, useRaster = TRUE, main = paste("Digit", label))

rsvd documentation built on Nov. 2, 2026, 5:06 p.m.