Skip to main content
v2026.11,858 entries · CC-BY 4.0

Scikit Learn Datasets: Load, Fetch & Generate

Learn how to use scikit learn datasets (sklearn.datasets): load toy data, fetch real-world corpora, generate synthetic samples, and manage local caching.

Written and maintained by CASRAI Editorial Board

Last updated

In empirical machine learning and data science research, standardized datasets provide the baseline benchmarks necessary to validate statistical algorithms, compare estimator behaviors, and verify analytical pipelines. The scikit learn datasets module (imported in Python as sklearn.datasets) provides a unified, production-tested suite of data utilities embedded directly within the scikit-learn ecosystem. Rather than requiring researchers to manually download, parse, and clean disparate raw files, sklearn datasets delivers standardized NumPy arrays and pandas DataFrames configured for immediate ingestion by estimators, transformers, and evaluation pipelines.

The sklearn.datasets architecture divides data access into three distinct functional interfaces: internal toy dataset loaders (load_*), external real-world dataset fetchers (fetch_*), and synthetic data generators (make_*). Understanding how each interface handles memory allocation, local disk caching, feature metadata, and return structures ensures strict adherence to computational reproducibility standards across computational experiments.

What it is used for in research

Researchers across biomedical informatics, computational social science, econometrics, and physical sciences utilize scikit learn datasets for several core methodological objectives:

  • Algorithm Benchmarking and Baseline Validation: When developing novel estimators or evaluating optimization heuristics, researchers require standard reference datasets with known dimensionalities, class distributions, and noise profiles. Built-in loaders such as load_iris() and load_digits() offer fixed, reproducible targets that eliminate confounding data-preparation variance between competing algorithms.
  • Pedagogical and Exploratory Scripting: In academic workshops and laboratory onboarding, the sklearn dataset utilities allow investigators to demonstrate statistical mechanics—such as principal component analysis or decision tree splitting—without managing complex external database connections or authentication keys. Researchers frequently pair these loaders with our scikit-learn tutorial to teach robust modeling workflows.
  • Controlled Sensitivity and Stress Testing: Real-world observational data rarely permits independent manipulation of collinearity, noise ratios, or class imbalance. The synthetic generators in datasets sklearn enable investigators to parametrically generate artificial feature matrices, testing how regression or classification algorithms degrade under controlled conditions.
  • Reproducible Pipeline Development: Research software components—including automated feature selectors and custom imputers—must be tested across diverse matrix topologies (dense, sparse, high-dimensional). Built-in fetchers provide standardized regression and classification benchmarks that integrate directly into continuous integration test suites.
  • Data Management and Provenance Tracking: Institutional funding mandates require transparent data curation. Aligning experimental code with standardized fetcher protocols supports an investigator’s institutional data management plan (DMP) by ensuring that public benchmark corpora can be programmatically re-acquired and verified.

Install or access (official source only)

The sklearn.datasets module is an integrated subpackage of scikit-learn, distributed under the open-source 3-Clause BSD license (Pedregosa et al., 2011). It does not exist as an independent standalone package; accessing it requires installing scikit-learn through official distribution channels.

Investigators should install or update scikit-learn within an isolated virtual environment (using venv or conda) on Python 3.9 or higher:

  • Python Package Index (PyPI): Run the standard installation command:
    pip install -U scikit-learn
  • Conda-Forge Ecosystem: For Conda or Mamba environments commonly deployed in scientific computing clusters:
    conda install -c conda-forge scikit-learn

Core dependencies required by scikit-learn include NumPy, SciPy, joblib, and threadpoolctl. To enable pandas DataFrame integration via the as_frame=True parameter, ensure that pandas is installed in the active environment (pip install pandas). Once installed, the module is imported into Python scripts via:

from sklearn import datasets
# or import specific loaders and generators
from sklearn.datasets import load_iris, fetch_california_housing, make_classification

Step-by-step task (numbered, with menu names or commands exactly as in the current docs)

The following five-step task demonstrates how to inspect available datasets, load a toy classification benchmark, configure return data types, fetch an external real-world dataset with local caching, and generate a synthetic classification matrix for algorithm stress testing.

  1. Load a toy dataset and inspect the Bunch object:

    Toy datasets reside locally within the scikit-learn package and require zero network access. Load the diagnostic breast cancer dataset and examine its container attributes:

    from sklearn.datasets import load_breast_cancer
    
    # Load the complete dataset container
    cancer = load_breast_cancer()
    
    # Inspect primary Bunch attributes
    print("Feature matrix shape:", cancer.data.shape)
    print("Target vector shape:", cancer.target.shape)
    print("Number of target classes:", len(cancer.target_names))
    print("Feature names sample:", cancer.feature_names[:3])
  2. Directly unpack features and targets with return_X_y:

    When metadata inspection is unnecessary and data must pass immediately into estimator training or cross-validation routines, pass return_X_y=True:

    from sklearn.datasets import load_iris
    
    # Directly unpack into NumPy arrays
    X, y = load_iris(return_X_y=True)
    
    print("Type of X:", type(X))
    print("Dimensions of X:", X.shape)
    print("Target classes:", set(y))
  3. Load data as native pandas DataFrames using as_frame:

    For tabular data analysis requiring column labels, missing value inspection, or exploratory plotting, enable as_frame=True:

    from sklearn.datasets import load_diabetes
    
    # Load as a pandas-compatible Bunch
    diabetes = load_diabetes(as_frame=True)
    
    # Access the combined DataFrame or individual frame components
    df_features = diabetes.data
    s_target = diabetes.target
    df_full = diabetes.frame
    
    print("Features DataFrame type:", type(df_features))
    print("Feature column names:", df_features.columns.tolist()[:4])
    print("First row values:
    ", df_features.iloc[0])
  4. Fetch an external real-world dataset with custom disk caching:

    Real-world datasets (such as California Housing or 20 Newsgroups) are downloaded on demand from public web archives and cached locally to prevent redundant downloads. Configure a dedicated project cache directory using the data_home parameter:

    import os
    from sklearn.datasets import fetch_california_housing
    
    # Define a custom local cache directory within the project scratchpad
    cache_dir = os.path.expanduser("./data_cache")
    
    housing = fetch_california_housing(data_home=cache_dir, as_frame=True)
    
    print("Housing records:", housing.data.shape[0])
    print("Median house value target mean:", housing.target.mean())

    Alternatively, set the system-wide environment variable SCIKIT_LEARN_DATA to control where fetchers persist downloaded corpora across cluster nodes.

  5. Generate a synthetic dataset with controlled properties:

    To evaluate how an algorithm handles non-linear boundaries or noisy labels, invoke a synthetic generator. For example, construct a binary classification task with specified informative features and random noise:

    from sklearn.datasets import make_classification
    
    X_synth, y_synth = make_classification(
        n_samples=1000,
        n_features=20,
        n_informative=10,
        n_redundant=5,
        n_clusters_per_class=2,
        flip_y=0.03,
        random_state=42
    )
    
    print("Synthetic feature matrix shape:", X_synth.shape)
    print("Class distribution:", {c: (y_synth == c).sum() for c in set(y_synth)})

    Synthetic data generated this way can immediately be passed to estimator tutorials, such as our scikit-learn decision tree guide, or partitioned using rigorous cross-validation in statistics.

Output and how to read it

The output returned by functions in the sklearn.datasets package depends on the parameters supplied during invocation. Understanding these container types enables robust integration into statistical modeling scripts.

1. The Bunch Object

By default, both load_* and fetch_* functions return a Bunch object, an internal scikit-learn container that inherits from the standard Python dict while exposing dictionary keys as object attributes (e.g., dataset.data is equivalent to dataset['data']). A standard Bunch contains:

  • .data: A 2D NumPy ndarray of shape (n_samples, n_features) representing the independent feature matrix. If as_frame=True was specified, this is a pandas.DataFrame.
  • .target: A 1D NumPy ndarray of shape (n_samples,) containing ground-truth values (categorical class indices for classification, continuous numerical values for regression). For multi-output tasks (such as load_linnerud()), this is a 2D array of shape (n_samples, n_targets).
  • .feature_names: A list of string labels corresponding to the columns in .data.
  • .target_names: A list of string labels corresponding to categorical classes in .target (classification datasets only).
  • .DESCR: A comprehensive plain-text string detailing the origin, history, variable definitions, and academic references for the dataset.
  • .frame: Present only when as_frame=True, containing a single unified pandas.DataFrame combining both features and target columns.
  • .filename: Path to the underlying CSV or data file stored on the local disk.

2. Tuple Unpacking: (X, y)

When return_X_y=True is passed to a loader or fetcher, or when any synthetic generator (make_*) is called, the function returns a 2-tuple (X, y) rather than a Bunch object:

X, y = make_classification(n_samples=500, n_features=10, random_state=42)
print("X type:", type(X), "shape:", X.shape)  # numpy.ndarray of shape (500, 10)
print("y type:", type(y), "shape:", y.shape)  # numpy.ndarray of shape (500,)

This streamlined tuple structure matches the standard method signatures expected across all scikit-learn estimators: model.fit(X, y) and cross_val_score(model, X, y).

Troubleshooting

When utilizing sklearn.datasets in scientific pipelines, researchers frequently encounter several operational failure modes:

1. ImportError or AttributeError with load_boston

Symptom: Attempting to execute from sklearn.datasets import load_boston raises ImportError: cannot import name 'load_boston' from 'sklearn.datasets'.

Cause: The Boston housing dataset was formally deprecated in scikit-learn version 1.0 and completely removed in version 1.2 due to ethical concerns regarding racial segregation variables encoded in the dataset. Modern versions of scikit-learn reject calls to this loader.

Resolution: Replace load_boston() with ethical, contemporary regression benchmarks. Scikit-learn officially recommends fetch_california_housing() or fetch_openml(name="ames_housing", as_frame=True) as direct drop-in replacements.

2. Network Timeouts and HTTP Errors during fetch_* Calls

Symptom: Executing fetch_california_housing() or fetch_20newsgroups() raises urllib.error.URLError, HTTPError: 503 Service Unavailable, or socket timeout exceptions.

Cause: The remote server hosting the raw dataset repository is temporarily unreachable, or an institutional firewall or proxy blocks outbound HTTP/HTTPS requests from computing nodes.

Resolution:

  • Pre-download datasets on an internet-enabled staging machine and copy the cache folder to the computing cluster. Specify the directory using data_home='/path/to/preloaded_cache'.
  • Configure the proxy environment variables http_proxy and https_proxy before launching Python sessions.
  • For OpenML datasets, use the exact numerical ID instead of the string name (e.g., fetch_openml(data_id=31)) to bypass search index latency.

3. Memory Exhaustion with High-Dimensional Fetchers

Symptom: Loading massive real-world datasets like fetch_rcv1() or fetch_kddcup99() results in an operating system MemoryError or process termination (SIGKILL).

Cause: Expanding compressed or sparse corpora into dense NumPy arrays exceeds available physical RAM.

Resolution: Keep sparse datasets in their native Compressed Sparse Row (CSR) format. Do not execute .toarray() on matrices returned by fetch_rcv1(). Use mini-batch estimators (such as SGDClassifier) and pass sliced chunks of data sequentially.

4. Data Leakage across Preprocessing and Splits

Symptom: Unusually high cross-validation performance that fails to replicate on independent experimental data.

Cause: Applying global scaling (such as z-score normalization) or feature selection to the entire matrix X returned by sklearn.datasets prior to executing train_test_split().

Resolution: Always partition raw matrices into training and testing splits first, or encapsulate transformers and estimators inside a sklearn.pipeline.Pipeline so transformations fit exclusively on training folds.

Free alternatives (table)

While sklearn.datasets is the established standard for general machine learning benchmarks in Python, complementary open-source libraries cater to specialized modalities such as natural language processing, computer vision, and distributed data streaming. The table below benchmarks free alternatives across structural characteristics:

Package / Tool Primary Scope Storage Format Offline Availability Ideal Research Application
scikit-learn (sklearn.datasets) Tabular, classical ML benchmarks, synthetic data NumPy ndarray, SciPy CSR, pandas DataFrame Full for toy datasets; cached for fetchers Statistical modeling, baseline benchmarking, algorithm development
Hugging Face Datasets (datasets) NLP, speech, multi-modal, large-scale scientific text Apache Arrow (memory-mapped, zero-copy) Cached locally; streaming mode supported Deep learning, transformer fine-tuning, large corpus text processing
Torchvision Datasets (torchvision.datasets) Computer vision, image classification, segmentation PyTorch Tensors, PIL Images Local download and disk caching Neural network vision pipelines, image augmentation benchmarks
TensorFlow Datasets (tfds) Multi-modal, cross-domain scientific corpora TFRecord format with graph streaming Local disk cache with hash verification TensorFlow / JAX deep learning models and distributed training
OpenML Python API (openml) Open science empirical benchmarking repository ARFF, Parquet, pandas DataFrame Local cache with automatic versioning Automated machine learning (AutoML) evaluations and meta-learning

FAQ

What is the difference between load_*, fetch_*, and make_* in sklearn datasets?

Functions prefixed with load_ access small toy datasets packaged directly within the scikit-learn installation files, requiring zero network access and minimal memory. Functions prefixed with fetch_ query external scientific repositories over the network to download larger real-world datasets, caching them locally in ~/scikit_learn_data. Functions prefixed with make_ do not load pre-existing data; instead, they generate artificial feature matrices using mathematical and statistical formulations with user-defined sample counts, noise levels, and dimensionalities.

How do I convert a scikit-learn Bunch object to a pandas DataFrame?

Modern scikit-learn loaders and fetchers support the as_frame=True parameter. Calling data = load_iris(as_frame=True) automatically configures data.data as a pandas.DataFrame containing feature columns labeled with their respective names, and data.target as a pandas.Series. If as_frame is not used, you can manually construct a DataFrame via pd.DataFrame(data.data, columns=data.feature_names).

Why was load_boston removed from scikit-learn?

The load_boston dataset was removed in scikit-learn 1.2 due to ethical concerns regarding systemic bias. The original 1978 dataset included a feature labeled ‘B’ measuring the proportion of Black residents by town, based on an assumption by the original authors that racial segregation influenced housing prices. The scikit-learn maintenance team determined that distributing this variable as a standard machine learning benchmark perpetuates harmful racial bias. Researchers are directed to fetch_california_housing or Ames housing via OpenML.

Where are fetched datasets stored on my computer?

By default, all fetch_* functions download raw data files into a directory named scikit_learn_data in your user home directory (e.g., ~/scikit_learn_data on Linux/macOS or C:Users<username>scikit_learn_data on Windows). You can override this location by supplying the data_home parameter directly to the fetch function or by setting the system environment variable SCIKIT_LEARN_DATA.

How does fetch_openml connect to the OpenML repository?

The fetch_openml() function issues REST API requests to openml.org to query and download public research datasets. You can specify a dataset by its unique name (e.g., fetch_openml('mnist_784', version=1)) or its definitive numeric identifier (e.g., fetch_openml(data_id=554)). The downloaded file is automatically converted into NumPy arrays or pandas DataFrames and cached locally.

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Scikit Learn Datasets: Load, Fetch & Generate

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →