Built-in and synthetic datasets

scikit-learn · Essentials · 7 min

Small, known datasets let you check that code works before you point it at real, messy data.

sklearn.datasets gives you data to experiment with in three flavours.

Toy datasets ship with the library and load instantly: load_iris, load_wine, load_breast_cancer (classification) and load_diabetes (regression). They return a Bunch, a dict with attribute access: .data, .target, .feature_names, .target_names and .DESCR. Two options make them nicer:

  • return_X_y=True returns just the (X, y) tuple,
  • as_frame=True returns pandas objects, with column names.

Real-world datasets such as fetch_california_housing or fetch_openml(name=...) download data on first use and cache it under ~/scikit_learn_data.

Generators create synthetic data with known structure: make_classification, make_regression, make_blobs (clusters), make_moons and make_circles (non-linear shapes). Pass random_state so you get the same data every run. Synthetic data is ideal for asking "does this model behave as I expect when the signal is X?" because you control the truth.

Toy datasets are tiny and clean, so scores on them say little about real problems. Use them for learning the API, debugging pipelines and writing reproducible examples, then move to your own data.

Example

from sklearn.datasets import load_iris, make_classification

iris = load_iris(as_frame=True)
print(iris.frame.shape)              # (150, 5)
print(iris.target_names.tolist())    # ['setosa', 'versicolor', 'virginica']

X, y = load_iris(return_X_y=True)
print(X.shape, y.shape)              # (150, 4) (150,)

X, y = make_classification(n_samples=500, n_features=6, n_informative=3,
                           weights=[0.9, 0.1], random_state=0)
print(X.shape, y.mean().round(2))    # (500, 6) 0.1

Remember

  • Toy loaders return a Bunch; use return_X_y=True or as_frame=True
  • make_* generators create synthetic data with a known structure
  • Always set random_state for reproducible examples

Watch out for

  • Treating a high score on a toy dataset as evidence a method works on real data.

Check yourself

  1. What does `load_iris(as_frame=True)` change?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: scikit-learn.org/stable/datasets.html