Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

load_digits

Loads scikit-learn’s built-in handwritten-digits classification dataset. It can return a metadata-rich Bunch or a features-and-target tuple, with optional class restriction and pandas output.

How it works

load_digits provides the built-in handwritten-digit classification dataset. By default it returns a Bunch containing flattened feature vectors, image-shaped data, labels, label names, and a description; options can restrict the classes, return a tuple, or produce pandas objects.

Syntax

sklearn.datasets.load_digits(*, n_class=10, return_X_y=False, as_frame=False)

Parameters

NameTypeDescription
n_classintNumber of classes to return; valid values are between 0 and 10. Default: 10.
return_X_yboolIf True, return (data, target) instead of a Bunch. Default: False.
as_frameboolIf True, return pandas data structures. Default: False.

Returns

By default, a Bunch containing the dataset. With return_X_y=True, returns (data, target); with as_frame=True, the data and target use pandas DataFrame or Series representations as documented.

Key points

  • The function supplies a ready-made dataset for supervised digit classification.
  • Every observation represents one digit image with eight pixels along each dimension.
  • Use n_class to limit the class set returned by the loader.
  • return_X_y=True provides the features and labels as a two-item result rather than structured dataset metadata.
  • as_frame=True represents the feature data as a pandas DataFrame with numeric column types.
  • The pandas target representation varies between Series and DataFrame according to its column count.

Common mistakes

  • n_class must be chosen within the documented 0–10 range.
  • When return_X_y is enabled, do not expect dataset attributes such as images or DESCR on the result.

Example: Load features and labels

from sklearn.datasets import load_digits

X, y = load_digits(return_X_y=True)
print(X.shape)
print(y.shape)

Loads the digits features and labels directly as a tuple, then prints their shapes.

When to use it

Classification experiments

Train and evaluate a classifier on a small built-in image dataset.

Image visualization

Inspect digit images using the returned image-shaped arrays.

Quick demonstrations

Run examples without downloading or preparing an external dataset.

Interview questions

QWhat does load_digits load, and what can it return?

The function loads a classification dataset in which each sample is an 8×8 digit image. Its default return value is a Bunch, while return_X_y=True gives the feature matrix and target separately.

Follow-up: What shape does the feature matrix use? Each image is flattened into 64 features, while the Bunch also exposes the image-shaped representation.

QWhat is the purpose and valid range of n_class?

n_class controls how many classes are returned and defaults to 10. The documented valid range is 0 through 10.

Follow-up: Why might a caller use fewer classes? To create a smaller, restricted classification task for experiments or demonstrations.

QHow do as_frame and return_X_y affect the result?

as_frame=True requests pandas representations: the data becomes a DataFrame, and the target becomes a DataFrame or Series depending on the number of target columns. With return_X_y=True, both tuple elements follow those pandas rules.

Follow-up: When would return_X_y=False be preferable? When metadata and additional dataset attributes available on the Bunch are useful.

  • sklearn.datasets.load_iris
  • sklearn.datasets.load_wine
  • sklearn.datasets.load_breast_cancer
  • sklearn.datasets.load_diabetes

Source: load_digits — scikit-learn 1.9.1 documentation. Checked 2026-10-15.