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

Scikit-Learn · Function

fetch_openml

Downloads a dataset from OpenML by name or numeric ID. It can cache the download and return either a dataset container or separate features and target.

How it works

fetch_openml identifies an OpenML dataset either by its numeric ID or by name, optionally constrained to a version. It downloads and caches the data, then returns either a Bunch containing dataset metadata and arrays or, when return_X_y is enabled, a two-item feature/target result; frame and parser options control representation and CSV parsing.

Syntax

sklearn.datasets.fetch_openml(name: str | None = None, *, version: str | int = 'active', data_id: int | None = None, data_home: str | PathLike | None = None, target_column: str | List | None = 'default-target', cache: bool = True, return_X_y: bool = False, as_frame: str | bool = 'auto', n_retries: int = 3, delay: float = 1.0, parser: str = 'auto', read_csv_kwargs: Dict | None = None)

Parameters

NameTypeDescription
namestr or None, default=NoneString identifier of the dataset.
versionstr or int, default='active'Dataset version; may be 'active' or an integer and requires name.
data_idint or None, default=NoneOpenML ID of the dataset.
data_homestr, path-like, or None, default=NoneAlternative download and cache directory.
target_columnstr, list, or None, default='default-target'Column name or column names to use as the target.
cachebool, default=TrueWhether to use the local cache.
return_X_ybool, default=FalseWhether to return features and target separately.
as_framestr or bool, default='auto'Whether to represent data as pandas objects; 'auto' selects automatically.
n_retriesint, default=3Number of download retries.
delayfloat, default=1.0Delay between retries.
parserstr, default='auto'CSV parsing mode.
read_csv_kwargsdict or None, default=NoneAdditional keyword arguments for CSV reading.

Returns

By default, a Bunch containing the loaded dataset and metadata; with return_X_y=True, returns the data and target separately.

Key points

  • Use the function to download datasets hosted in OpenML.
  • A dataset can be selected with a numeric ID or with its name plus an optional version.
  • data_id provides the most precise dataset selection.
  • Set data_home when downloads and cached files should live outside the default location.
  • Without data_home, the cache uses subdirectories below the user’s scikit-learn data directory.
  • The name parameter is the OpenML dataset’s string identifier.

Common mistakes

  • Choose exactly one identifier: supplying both name and data_id is invalid.
  • The version argument requires a dataset name; use data_id alone when retrieving by numeric identifier.
  • The default active selection is not necessarily the newest dataset version, so pin a numeric version for reproducibility.

Example: Load a versioned dataset

from sklearn.datasets import fetch_openml

X, y = fetch_openml(
    name="mnist_784",
    version=1,
    return_X_y=True,
    as_frame=False,
)
print(X.shape, y.shape)

Downloads a specific OpenML dataset version and returns its features and target as separate objects.

When to use it

Download benchmark data

A notebook or script needs a public benchmark dataset by its OpenML name.

Pin a dataset version

A pipeline must use exactly the dataset revision used during development.

Reuse cached data

Repeated local experiments should avoid downloading the same files again.

Load data as frames

A model workflow needs tabular pandas-compatible inputs and outputs.

Interview questions

QHow does fetch_openml identify a dataset?

It accepts either a dataset name or an OpenML data_id, but not both. A name may also be paired with a version; data_id is the most specific retrieval method.

Follow-up: How would you make a training job reproducible? Pin the dataset’s numeric version, and use a stable cache location if appropriate.

QWhat does the return_X_y option change?

By default, the function returns a Bunch-like container with the downloaded dataset and related information. Setting return_X_y=True instead returns the feature data and target separately.

Follow-up: When would as_frame=True be useful? Use it when pandas-compatible DataFrame and Series representations are preferable, especially for heterogeneous columns.

QWhere does fetch_openml cache downloaded data?

data_home selects the download and cache directory. If it is omitted, scikit-learn stores downloaded data under subdirectories of ~/scikit_learn_data.

Follow-up: What operational concern follows from caching? A shared or persistent cache can avoid repeated downloads, while an explicit data_home makes the location predictable.

  • sklearn.datasets.load_iris
  • sklearn.datasets.get_data_home
  • sklearn.datasets.clear_data_home

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