Implementation:Scikit learn Scikit learn Load Iris
Appearance
| Field | Value |
|---|---|
| source | scikit-learn|https://github.com/scikit-learn/scikit-learn |
| domains | Data_Science |
| last_updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for loading the Iris flower dataset provided by scikit-learn.
Description
The load_iris function returns the classic Iris flower dataset, originally collected by R.A. Fisher in 1936. The dataset contains:
- 150 samples (50 per class)
- 4 numeric features: sepal length (cm), sepal width (cm), petal length (cm), petal width (cm)
- 3 target classes: setosa, versicolor, virginica
All features are real-valued and positive. The dataset is bundled with scikit-learn and requires no network access.
Usage
- Quick classification benchmarks to verify that a new algorithm works correctly on a well-understood problem.
- Tutorials and teaching examples where a small, clean dataset is needed.
- Unit tests and integration tests for scikit-learn-based pipelines.
- Exploratory data analysis and visualization demonstrations.
Code Reference
Source Location
sklearn/datasets/_base.py, function load_iris
Signature
def load_iris(*, return_X_y=False, as_frame=False):
Import
from sklearn.datasets import load_iris
I/O Contract
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
return_X_y |
bool | False |
If True, returns a (data, target) tuple instead of a Bunch object.
|
as_frame |
bool | False |
If True, returns data as a pandas DataFrame and target as a pandas Series.
|
Outputs
| Condition | Return Type | Description |
|---|---|---|
return_X_y=False (default) |
sklearn.utils.Bunch |
Dictionary-like object with keys: data (ndarray of shape (150, 4)), target (ndarray of shape (150,)), feature_names (list of 4 strings), target_names (ndarray of shape (3,)), DESCR (str), filename (str), frame (DataFrame or None).
|
return_X_y=True |
tuple(ndarray, ndarray) |
A tuple (data, target) where data has shape (150, 4) and target has shape (150,).
|
as_frame=True |
DataFrame / Series | When combined with return_X_y=True, both elements are pandas objects. When combined with return_X_y=False, the Bunch's data and target attributes are pandas objects and frame is a full DataFrame of shape (150, 5).
|
Usage Examples
Basic loading as a Bunch:
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data.shape) # (150, 4)
print(iris.target.shape) # (150,)
print(iris.target_names) # ['setosa' 'versicolor' 'virginica']
print(iris.feature_names) # ['sepal length (cm)', 'sepal width (cm)', ...]
Loading as X, y arrays directly:
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
print(X.shape) # (150, 4)
print(y.shape) # (150,)
Loading as a pandas DataFrame:
from sklearn.datasets import load_iris
iris = load_iris(as_frame=True)
print(type(iris.data)) # <class 'pandas.core.frame.DataFrame'>
print(iris.data.head())
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment