Skip to content

check_grid_search_param_grid_validation

yohou.testing.search.check_grid_search_param_grid_validation(search_cv)

Check param_grid format is validated (dict or list of dicts).

Parameters

Name Type Description Default
search_cv GridSearchCV

GridSearchCV instance

required

Raises

Type Description
AssertionError

If param_grid format is not validated correctly

Source Code

Show/Hide source
def check_grid_search_param_grid_validation(search_cv) -> None:
    """Check param_grid format is validated (dict or list of dicts).

    Parameters
    ----------
    search_cv : GridSearchCV
        GridSearchCV instance

    Raises
    ------
    AssertionError
        If param_grid format is not validated correctly

    """
    if not isinstance(search_cv, GridSearchCV):
        raise ValueError("This check requires GridSearchCV instance")

    param_grid = search_cv.param_grid

    # Check param_grid is dict or list of dicts
    if isinstance(param_grid, dict):
        # Valid: single dict
        for key, value in param_grid.items():
            assert isinstance(key, str), f"param_grid keys should be str, got {type(key)}"
            assert isinstance(value, list | tuple), f"param_grid values should be list or tuple, got {type(value)}"
    elif isinstance(param_grid, list):
        # Valid: list of dicts
        for grid in param_grid:
            assert isinstance(grid, dict), f"param_grid list elements should be dict, got {type(grid)}"
            for key, value in grid.items():
                assert isinstance(key, str), f"param_grid keys should be str, got {type(key)}"
                assert isinstance(value, list | tuple), f"param_grid values should be list or tuple, got {type(value)}"
    else:
        raise AssertionError(f"param_grid should be dict or list of dicts, got {type(param_grid)}")