Implementation:Scikit learn Scikit learn ParameterGrid Init
Template:Implementation Metadata
Overview
Concrete tool for generating exhaustive parameter combinations from a grid specification provided by scikit-learn.
The ParameterGrid class takes a dictionary (or list of dictionaries) mapping parameter names to sequences of allowed values, and produces an iterable over all combinations via the Cartesian product. It supports indexing, length queries, and deterministic iteration order.
Code Reference
Class Signature
class ParameterGrid:
def __init__(self, param_grid):
...
Constructor Parameter
| Parameter | Type | Description |
|---|---|---|
param_grid |
dict of str to sequence, or sequence of such dicts |
The parameter grid to explore. Each dictionary maps estimator parameter names (strings) to sequences of allowed values. A sequence of dicts specifies multiple sub-grids to search, which is useful for avoiding nonsensical parameter combinations. |
I/O Contract
Input:
- A single dictionary
{'param_name': [val1, val2, ...], ...}or a list of such dictionaries. - Each value sequence must be a non-empty list or numpy array (not a string, not a scalar).
- Numpy arrays must be one-dimensional.
Output (via iteration):
- Yields dictionaries mapping each parameter name to one of its allowed values.
- The total number of yielded dictionaries equals the sum of the Cartesian product sizes for each sub-grid.
- Keys are sorted alphabetically within each yielded dictionary for reproducibility.
Protocols supported:
__iter__-- iterate over all parameter combinations__len__-- return the total number of combinations__getitem__(ind)-- return the ind-th combination (enables memory-efficient random sampling without replacement)
Validation Rules
The constructor validates the input and raises:
TypeErrorifparam_gridis not a dict or list of dicts.TypeErrorif any value is a string or is not a list/numpy array.ValueErrorif any numpy array has more than one dimension.ValueErrorif any value sequence is empty.
Usage Examples
Single Grid
from sklearn.model_selection import ParameterGrid
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
grid = ParameterGrid(param_grid)
print(len(grid)) # 6 (3 * 2)
for params in grid:
print(params)
# {'C': 0.1, 'kernel': 'linear'}
# {'C': 0.1, 'kernel': 'rbf'}
# {'C': 1, 'kernel': 'linear'}
# {'C': 1, 'kernel': 'rbf'}
# {'C': 10, 'kernel': 'linear'}
# {'C': 10, 'kernel': 'rbf'}
Multiple Sub-grids
from sklearn.model_selection import ParameterGrid
# Avoid nonsensical combinations: gamma only applies to rbf kernel
param_grid = [
{'kernel': ['linear'], 'C': [1, 10]},
{'kernel': ['rbf'], 'C': [1, 10], 'gamma': [0.1, 1]}
]
grid = ParameterGrid(param_grid)
print(len(grid)) # 2 + 4 = 6
print(grid[3]) # Indexing: {'C': 10, 'gamma': 0.1, 'kernel': 'rbf'}
Randomized Alternative: ParameterSampler
For randomized search over parameter spaces, scikit-learn provides ParameterSampler as the counterpart to ParameterGrid.
class ParameterSampler:
def __init__(self, param_distributions, n_iter, *, random_state=None):
...
| Parameter | Type | Description |
|---|---|---|
param_distributions |
dict or list of dicts |
Maps parameter names to distributions (objects with an rvs method, such as scipy.stats distributions) or lists of discrete values.
|
n_iter |
int |
Number of parameter settings to sample. |
random_state |
int, RandomState, or None |
Controls reproducibility. |
Key behaviors:
- If all parameters are lists, sampling is done without replacement from the full grid.
- If at least one parameter has a distribution with an
rvsmethod, sampling is done with replacement. - A warning is issued if
n_iterexceeds the total grid size when all parameters are lists.