Skip to content

Better error message needed when accidentally supplying scalar value to parameter tuning classes #12640

Closed

Description

@readyready15728

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

  1. added
    Easy
    on Nov 21, 2018
  2. amueller commented on Nov 21, 2018

    @amueller
    Member

    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.

  3. readyready15728 commented on Nov 21, 2018

    @readyready15728
    Author

    I don't know what other term to use tbh

  4. amueller commented on Nov 21, 2018

    @amueller
    Member

    I mean I used "single value", not sure if that would be clear to you?

  5. readyready15728 commented on Nov 21, 2018

    @readyready15728
    Author

    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".

    if type(value) == str:
        term = 'string'
    else if type(value) = float:
        term = 'float'
    # ...
    else:
        term = 'single value'

    That might be a little ungainly though.

  6. readyready15728 commented on Nov 21, 2018

    @readyready15728
    Author

    Again I also suggested automatic wrapping of scalars and I think that's even better but it's your call.

  7. amueller commented on Nov 21, 2018

    @amueller
    Member

    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.

  8. readyready15728 commented on Nov 21, 2018

    @readyready15728
    Author

    We could make a separate message for string and otherwise use "single item of type {}".format(type).

    Oh yeah, that's even better. Maybe also make a special case for int (as "integer") but otherwise more compact and efficient.

    Automatic wrapping? you mean making them a list?

    Yes.

    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.

    I'm not sure what you mean but it looks like we're on track to some kind of decent solution in any case.

  9. amueller commented on Nov 22, 2018

    @amueller
    Member

    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.

  10. wenliwen64 commented on Nov 22, 2018

    @wenliwen64

    @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.

  11. jnothman commented on Nov 26, 2018

    @jnothman
    Member
  12. readyready15728 commented on Nov 26, 2018

    @readyready15728
    Author

    You make a good case. Sounds good.

  13. jnothman commented on Nov 26, 2018

    @jnothman
    Member
  14. amueller commented on Nov 27, 2018

    @amueller
    Member

    @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 ;)

  15. MichaelShoemaker commented on Mar 30, 2020

    @MichaelShoemaker
    Contributor

    Hello. I'm new to contributing to OSS. Would it be okay if I attempt to do the rewording of this message?

    ValueError: Parameter grid for parameter (colsample_bytree) needs to be a list or numpy array, but got (<class 'int'>). Single values need to be wrapped in a list with one element.

  16. jnothman commented on Mar 31, 2020

    @jnothman
    Member

    @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

  17. MichaelShoemaker commented on Apr 1, 2020

    @MichaelShoemaker
    Contributor

    @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?

  18. MichaelShoemaker commented on Apr 13, 2020

    @MichaelShoemaker
    Contributor

    @jnothman These changes were merged. I believe this issue can be closed.

  19. thomasjpfan commented on Apr 13, 2020

    @thomasjpfan
    Member

    Closing since this was fixed in #16861

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Easyhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions