Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MRG allow empty grid in ParameterGrid #2082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions sklearn/grid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,30 @@ class ParameterGrid(object):

Parameters
----------
param_grid : dict of string to sequence
param_grid : dict of string to sequence, or sequence of such
The parameter grid to explore, as a dictionary mapping estimator
parameters to sequences of allowed values.

An empty dict signifies default parameters.

A sequence of dicts signifies a sequence of grids to search, and is
useful to avoid exploring parameter combinations that make no sense
or have no effect. See the examples below.

Examples
--------
>>> from sklearn.grid_search import ParameterGrid
>>> param_grid = {'a':[1, 2], 'b':[True, False]}
>>> param_grid = {'a': [1, 2], 'b': [True, False]}
>>> list(ParameterGrid(param_grid)) #doctest: +NORMALIZE_WHITESPACE
[{'a': 1, 'b': True}, {'a': 1, 'b': False},
{'a': 2, 'b': True}, {'a': 2, 'b': False}]

>>> grid = [{'kernel': ['linear']}, {'kernel': ['rbf'], 'gamma': [1, 10]}]
>>> list(ParameterGrid(grid)) #doctest: +NORMALIZE_WHITESPACE
[{'kernel': 'linear'},
{'kernel': 'rbf', 'gamma': 1},
{'kernel': 'rbf', 'gamma': 10}]

See also
--------
:class:`GridSearchCV`:
Expand All @@ -81,16 +93,19 @@ def __iter__(self):
for p in self.param_grid:
# Always sort the keys of a dictionary, for reproducibility
items = sorted(p.items())
keys, values = zip(*items)
for v in product(*values):
params = dict(zip(keys, v))
yield params
if not items:
yield {}
else:
keys, values = zip(*items)
for v in product(*values):
params = dict(zip(keys, v))
yield params

def __len__(self):
"""Number of points on the grid."""
# Product function that can handle iterables (np.product can't).
product = partial(reduce, operator.mul)
return sum(product(len(v) for v in p.values())
return sum(product(len(v) for v in p.values()) if p else 1
for p in self.param_grid)


Expand Down
9 changes: 9 additions & 0 deletions sklearn/tests/test_grid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ def test_parameter_grid():
set(("foo", x, "bar", y)
for x, y in product(params2["foo"], params2["bar"])))

# Special case: empty grid (useful to get default estimator settings)
empty = ParameterGrid({})
assert_equal(len(empty), 1)
assert_equal(list(empty), [{}])

has_empty = ParameterGrid([{'C': [1, 10]}, {}])
assert_equal(len(has_empty), 3)
assert_equal(list(has_empty), [{'C': 1}, {'C': 10}, {}])


def test_grid_search():
"""Test that the best estimator contains the right value for foo_param"""
Expand Down