Implementation:Scikit learn Scikit learn Bunch
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Data Structures |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for providing a dictionary-like container that exposes keys as attributes, provided by scikit-learn.
Description
The Bunch class extends Python's dict by enabling values to be accessed either by key (bunch["value_key"]) or by attribute (bunch.value_key). It is used throughout scikit-learn as the return type for dataset loading functions and other utilities. It also supports deprecated keys with warning messages.
Usage
Use this container when you need a dictionary that also supports attribute-style access, typically when returning structured data from functions. It is the standard return type for load_iris(), fetch_california_housing(), and other dataset loaders.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/utils/_bunch.py
Signature
class Bunch(dict):
def __init__(self, **kwargs):
def __getitem__(self, key):
def __setattr__(self, key, value):
def __getattr__(self, key):
def _set_deprecated(self, value, *, new_key, deprecated_key, warning_message):
Import
from sklearn.utils import Bunch
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| **kwargs | any | No | Key-value pairs to populate the Bunch |
Outputs
| Name | Type | Description |
|---|---|---|
| bunch | Bunch | Dictionary-like object with attribute-style access |
Usage Examples
Basic Usage
from sklearn.utils import Bunch
b = Bunch(a=1, b=2)
print(b['b']) # 2
print(b.b) # 2
b.c = 6
print(b['c']) # 6