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

Scikit-Learn · Function

load_iris

Loads scikit-learn’s built-in Iris dataset for multi-class classification. It returns a metadata-rich Bunch by default, or data and targets directly with configurable pandas output.

Since
0.18

How it works

load_iris provides the built-in Iris classification dataset. By default it returns a dictionary-like Bunch containing feature data, labels, names, and a description; optional flags can instead return feature and target objects directly and use pandas containers.

Syntax

sklearn.datasets.load_iris(*, return_X_y=False, as_frame=False)

Parameters

NameTypeDescription
return_X_ybool, default=FalseWhether to return a tuple of data and target instead of a Bunch object.
as_framebool, default=FalseWhether to return pandas DataFrame or Series objects with appropriate dtypes.

Returns

By default, a Bunch dictionary-like object containing the dataset and metadata. 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 the built-in Iris dataset for a classification task.
  • Iris is presented as a classic, easy multi-class classification dataset.
  • The feature component is two-dimensional, with rows representing samples and columns representing features.
  • Rows correspond to individual observations, while columns correspond to measured features.
  • return_X_y=True provides data and target directly as a two-item tuple rather than the metadata-rich Bunch.
  • as_frame=True makes the feature data a pandas DataFrame with numeric column dtypes.

Common mistakes

  • Setting return_X_y=True changes the return type from one Bunch to a two-item tuple, so code must unpack the result.
  • With as_frame=True, the target's pandas type depends on its number of columns; do not assume it is always a DataFrame.

Example: Load Iris as a DataFrame

from sklearn.datasets import load_iris

iris = load_iris(as_frame=True)
print(iris.data.shape)
print(iris.target_names.tolist())

Loads the Iris dataset with feature data in a pandas DataFrame and prints its dimensions and class names.

When to use it

Classifier prototyping

Quickly obtain a small, well-known labeled dataset for testing a classifier.

Tabular exploration

Use named numeric columns and pandas operations during exploratory analysis.

Teaching and demonstrations

Demonstrate dimensionality reduction or visualization on a standard dataset.

Interview questions

QWhat does load_iris return by default?

The function loads the Iris dataset for classification and, by default, returns a Bunch. The Bunch exposes feature data, classification targets, names, and dataset documentation.

Follow-up: How would you obtain pandas objects? Call load_iris(as_frame=True); the feature data becomes a pandas DataFrame and the target is a pandas DataFrame or Series as documented.

QWhat is the purpose of return_X_y?

return_X_y=False is the default and returns the complete Bunch. Setting it to True returns a tuple containing data followed by target, which is convenient when metadata is unnecessary.

Follow-up: What happens if both return_X_y=True and as_frame=True are used? The tuple elements are pandas DataFrames or Series as described by the documentation.

QWhat does as_frame change?

as_frame=True requests pandas representations with appropriate numeric column dtypes. This is useful when downstream code expects DataFrame-style labels and tabular operations.

Follow-up: Does as_frame=True alone remove the Bunch? No; return_X_y controls whether the result is a tuple, while as_frame controls the representations of its data and target components.

  • sklearn.datasets.load_wine
  • sklearn.datasets.load_digits
  • The Iris Dataset
  • Principal Component Analysis (PCA) on Iris Dataset

Source: sklearn.datasets.load_iris — scikit-learn 1.9.1 documentation. Checked 2026-10-15.