Classification experiments
Train and evaluate a classifier on a small built-in image dataset.
Scikit-Learn · Function
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.
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.
sklearn.datasets.load_digits(*, n_class=10, return_X_y=False, as_frame=False)| Name | Type | Description |
|---|---|---|
n_class | int | Number of classes to return; valid values are between 0 and 10. Default: 10. |
return_X_y | bool | If True, return (data, target) instead of a Bunch. Default: False. |
as_frame | bool | If True, return pandas data structures. Default: False. |
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.
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.
Train and evaluate a classifier on a small built-in image dataset.
Inspect digit images using the returned image-shaped arrays.
Run examples without downloading or preparing an external dataset.
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.
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.
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_irissklearn.datasets.load_winesklearn.datasets.load_breast_cancersklearn.datasets.load_diabetes