@@ -94,7 +94,8 @@ class ParameterGrid:
9494 def __init__ (self , param_grid ):
9595 if not isinstance (param_grid , (Mapping , Iterable )):
9696 raise TypeError (
97- "Parameter grid is not a dict or a list ({!r})" .format (param_grid )
97+ f"Parameter grid should be a dict or a list, got: { param_grid !r} of"
98+ f" type { type (param_grid ).__name__ } "
9899 )
99100
100101 if isinstance (param_grid , Mapping ):
@@ -105,12 +106,26 @@ def __init__(self, param_grid):
105106 # check if all entries are dictionaries of lists
106107 for grid in param_grid :
107108 if not isinstance (grid , dict ):
108- raise TypeError ("Parameter grid is not a dict ({!r})" .format (grid ))
109- for key in grid :
110- if not isinstance (grid [key ], Iterable ):
109+ raise TypeError (f"Parameter grid is not a dict ({ grid !r} )" )
110+ for key , value in grid .items ():
111+ if isinstance (value , np .ndarray ) and value .ndim > 1 :
112+ raise ValueError (
113+ f"Parameter array for { key !r} should be one-dimensional, got:"
114+ f" { value !r} with shape { value .shape } "
115+ )
116+ if isinstance (value , str ) or not
E7F5
isinstance (
117+ value , (np .ndarray , Sequence )
118+ ):
111119 raise TypeError (
112- "Parameter grid value is not iterable "
113- "(key={!r}, value={!r})" .format (key , grid [key ])
120+ f"Parameter grid for parameter { key !r} needs to be a list or a"
121+ f" numpy array, but got { value !r} (of type "
122+ f"{ type (value ).__name__ } ) instead. Single values "
123+ "need to be wrapped in a list with one element."
124+ )
125+ if len (value ) == 0 :
126+ raise ValueError (
127+ f"Parameter grid for parameter { key !r} need "
128+ f"to be a non-empty sequence, got: { value !r} "
114129 )
115130
116131 self .param_grid = param_grid
@@ -244,9 +259,9 @@ class ParameterSampler:
244259 def __init__ (self , param_distributions , n_iter , * , random_state = None ):
245260 if not isinstance (param_distributions , (Mapping , Iterable )):
246261 raise TypeError (
247- "Parameter distribution is not a dict or a list ({!r})" . format (
248- param_distributions
249- )
262+ "Parameter distribution is not a dict or a list,"
263+ f" got: { param_distributions !r } of type "
264+ f" { type ( param_distributions ). __name__ } "
250265 )
251266
252267 if isinstance (param_distributions , Mapping ):
@@ -264,8 +279,8 @@ def __init__(self, param_distributions, n_iter, *, random_state=None):
264279 dist [key ], "rvs"
265280 ):
266281 raise TypeError (
267- "Parameter value is not iterable "
268- "or distribution (key={!r}, value={!r})" . format ( key , dist [key ])
282+ f "Parameter grid for parameter { key !r } is not iterable "
283+ f "or a distribution (value={ dist [key ]} )"
269284 )
270285 self .n_iter = n_iter
271286 self .random_state = random_state
@@ -321,30 +336,6 @@ def __len__(self):
321336 return self .n_iter
322337
323338
324- def _check_param_grid (param_grid ):
325- if hasattr (param_grid , "items" ):
326- param_grid = [param_grid ]
327-
328- for p in param_grid :
329- for name , v in p .items ():
330- if isinstance (v , np .ndarray ) and v .ndim > 1 :
331- raise ValueError ("Parameter array should be one-dimensional." )
332-
333- if isinstance (v , str ) or not isinstance (v , (np .ndarray , Sequence )):
334- raise ValueError (
335- "Parameter grid for parameter ({0}) needs to"
336- " be a list or numpy array, but got ({1})."
337- " Single values need to be wrapped in a list"
338- " with one element." .format (name , type (v ))
339- )
340-
341- if len (v ) == 0 :
342- raise ValueError (
343- "Parameter values for parameter ({0}) need "
344- "to be a non-empty sequence." .format (name )
345- )
346-
347-
348339def _check_refit (search_cv , attr ):
349340 if not search_cv .refit :
350341 raise AttributeError (
@@ -1385,7 +1376,6 @@ def __init__(
13851376 return_train_score = return_train_score ,
13861377 )
13871378 self .param_grid = param_grid
1388- _check_param_grid (param_grid )
13891379
13901380 def _run_search (self , evaluate_candidates ):
13911381 """Search all candidates in param_grid"""
0 commit comments