Repository navigation
Description
Description
As with issue #12621, the hyperparameter optimizers are not very helpful when one (for example) supplies a string as an element in the supplied param_grid.
Steps/Code to Reproduce
Here's what did it for me
# Parameters suggested by previous top scorers from a RandomizedSearchCV
param_grid = {'colsample_bytree': [1],
'learning_rate': [0.05, 0.1, 0.3],
'max_depth': [5, 8],
'n_estimators': [100]}
tree_model = GridSearchCV(xgb.XGBRegressor(), param_grid, cv=3, verbose=100)
tree_model.fit(X_train, y_train)
# And now the linear approach
param_grid['booster'] = ['gblinear']
linear_model = GridSearchCV(xgb.XGBRegressor(), param_grid, cv=3, verbose=100)
linear_model.fit(X_train, y_train)Expected Results
Something along the lines of: "Scalar value supplied in param_grid needs to be wrapped in a list or array of one element."
It doesn't seem like a huge change but it draws direct attention to the issue rather than having the user wonder why the error is happening. IIRC a fairly substantial amount of Python code will accept a scalar just as well as a list or similar so the way things are now may defy user expectations. And in fact if you start typing "parameter values for parameter" into Google there is at least one autocomplete result which suggests that a lot of people have had this issue. A number of Stack Exchange posts also appear if the search is carried out. I tend to think that others are thinking what I did: "But booster is supposed to be a string!"
Alternatively, accepting the scalar value and wrapping it automatically might be even better.
Actual Results
Traceback (most recent call last):
File "predict.py", line 48, in <module>
linear_model = GridSearchCV(xgb.XGBRegressor(), param_grid, cv=3, verbose=100)
File "/usr/local/lib/python3.5/dist-packages/sklearn/model_selection/_search.py", line 1187, in __init__
_check_param_grid(param_grid)
File "/usr/local/lib/python3.5/dist-packages/sklearn/model_selection/_search.py", line 379, in _check_param_grid
" np.ndarray.".format(name))
ValueError: Parameter values for parameter (booster) need to be a sequence(but not a string) or np.ndarray.
Versions
I have now upgraded from Ubuntu's obsolete 0.19 package:
System
------
python: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
machine: Linux-4.4.0-137-generic-i686-with-Ubuntu-16.04-xenial
executable: /usr/bin/python3
BLAS
----
macros: HAVE_CBLAS=None
cblas_libs: openblas, openblas
lib_dirs: /usr/lib
Python deps
-----------
sklearn: 0.20.0
scipy: 1.0.1
pip: 10.0.0
Cython: 0.28.1
pandas: 0.23.0.dev0+708.gc4b4a81.dirty
numpy: 1.14.2
setuptools: 39.0.1
Activity
amueller commentedon Nov 21, 2018
Feel free to improve the value error. This error tries to address what you're saying but it's not very clear indeed.
Maybe "Parameter grid for parameter {} needs to be a list or numpy array, but got {}. Single values need to be wrapped in a list with one element."?
Happy with "scalar" as well but might be harder to understand for some.
readyready15728 commentedon Nov 21, 2018
I don't know what other term to use tbh
amueller commentedon Nov 21, 2018
I mean I used "single value", not sure if that would be clear to you?
readyready15728 commentedon Nov 21, 2018
Oh OK yes. That works I guess. (FWIW I am a native English speaker so language is not an impediment.)
What I was thinking about in the shower though is doing an if-else block where typical suspects are used to try to choose a term and on the last else clause the default is "single value" or "single object".
That might be a little ungainly though.
readyready15728 commentedon Nov 21, 2018
Again I also suggested automatic wrapping of scalars and I think that's even better but it's your call.
amueller commentedon Nov 21, 2018
We could make a separate message for string and otherwise use "single item of type {}".format(type).
Automatic wrapping? you mean making them a list?
I thought about that and I wonder if there's weird edge-cases when that's not good. I guess if someone changes a single value to something that's supposed to be a singleton but the type is
{'thing': value}and value used to be scalar but then becomes a list, then instead of passing the whole list it will iterate over it and try all values.
readyready15728 commentedon Nov 21, 2018
Oh yeah, that's even better. Maybe also make a special case for
int(as "integer") but otherwise more compact and efficient.Yes.
I'm not sure what you mean but it looks like we're on track to some kind of decent solution in any case.
amueller commentedon Nov 22, 2018
Basically I would like to support passing scalars but I'm worried that there are cases when that will make the behavior harder to understand and result in hard-to-debug behavior. But we should fix the message anyway.
wenliwen64 commentedon Nov 22, 2018
@readyready15728 Are you working on this? If you don't mind, I would like to create a pr which fixes this by simply using the wording suggested by @amueller.
ENH improve error message for bad parameter grid (scikit-learn#12640)
jnothman commentedon Nov 26, 2018
readyready15728 commentedon Nov 26, 2018
You make a good case. Sounds good.
jnothman commentedon Nov 26, 2018
amueller commentedon Nov 27, 2018
@jnothman we could try wrap everything that we can't identify as a list but I agree it's fragile and we shouldn't try it ;)
MichaelShoemaker commentedon Mar 30, 2020
Hello. I'm new to contributing to OSS. Would it be okay if I attempt to do the rewording of this message?
jnothman commentedon Mar 31, 2020
@MichaelShoemaker Please try to identify the code where you can check for this case and raise the proposed error. Then you can create a pull request. Let us know if you need support along the way
MichaelShoemaker commentedon Apr 1, 2020
@jnothman Submitted. Fingers crossed! This is my first time doing an "actual" pull request. Kind of nervous... By chance do you know any good Discord or Slack Channels for Github noobs?
MichaelShoemaker commentedon Apr 13, 2020
@jnothman These changes were merged. I believe this issue can be closed.
thomasjpfan commentedon Apr 13, 2020
Closing since this was fixed in #16861