Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Version incompatibility with latest scikit-learn. #902

Closed

Description

@ytsmiling

python -c "import skopt" fails when scikit-learn==0.23.0 is installed.

Error messages

ImportError: cannot import name 'MaskedArray' from 'sklearn.utils.fixes' (~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/sklearn/utils/fixes.py)

Environment

I reproduced the problem at least in the following python versions
python: 3.6.6, 3.8.2

Minimum code to reproduce

pip install scikit-learn==0.23.0 scikit-optimize==0.7.4
python -c "import skopt"

Activity

  1. Innixma commented on May 12, 2020

    @Innixma

    Can confirm that this breaks autogluon<=0.0.8 with import autogluon

  2. Innixma commented on May 13, 2020

    @Innixma

    Issue on scikit-learn: scikit-learn/scikit-learn#17213

  3. rth commented on May 13, 2020

    @rth

    A temporary workaround for people experiencing this would be to monkeypatch the missing import (assuming numpy >=1.13.3),

    from numpy.ma import MaskedArray
    import sklearn.utils.fixes
    
    sklearn.utils.fixes.MaskedArray = MaskedArray
    
    import skopt

    A proper fix will be included in scikit-learn 0.23.1 (or if scikit-optimize makes a release with a fix from #904 before).

  4. magic-lantern commented on May 19, 2020

    @magic-lantern

    I am using skopt 0.7.4 and scikit-learn 0.23.1 and looks like 0.23.1 introduced some new errors.

    Here's some example code taken straight from the website at https://scikit-optimize.github.io/stable/auto_examples/sklearn-gridsearchcv-replacement.html#minimal-example:

    from skopt import BayesSearchCV
    from sklearn.datasets import load_digits
    from sklearn.svm import SVC
    from sklearn.model_selection import train_test_split
    
    X, y = load_digits(10, True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, test_size=.25, random_state=0)
    
    # log-uniform: understand as search over p = exp(x) by varying x
    opt = BayesSearchCV(
        SVC(),
        {
            'C': (1e-6, 1e+6, 'log-uniform'),
            'gamma': (1e-6, 1e+1, 'log-uniform'),
            'degree': (1, 8),  # integer valued parameter
            'kernel': ['linear', 'poly', 'rbf'],  # categorical parameter
        },
        n_iter=32,
        cv=3
    )
    
    opt.fit(X_train, y_train)
    
    print("val. score: %s" % opt.best_score_)
    print("test score: %s" % opt.score(X_test, y_test))

    When I run this example, I get the following output:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    in 
        20 )
        21 
    ---> 22 opt.fit(X_train, y_train)
        23 
        24 print("val. score: %s" % opt.best_score_)
    
    /opt/anaconda3/envs/py37/lib/python3.7/site-packages/skopt/searchcv.py in fit(self, X, y, groups, callback)
        678                 optim_result = self._step(
        679                     X, y, search_space, optimizer,
    --> 680                     groups=groups, n_points=n_points_adjusted
        681                 )
        682                 n_iter -= n_points
    
    /opt/anaconda3/envs/py37/lib/python3.7/site-packages/skopt/searchcv.py in _step(self, X, y, search_space, optimizer, groups, n_points)
        564         refit = self.refit
        565         self.refit = False
    --> 566         self._fit(X, y, groups, params_dict)
        567         self.refit = refit
        568 
    
    /opt/anaconda3/envs/py37/lib/python3.7/site-packages/skopt/searchcv.py in _fit(self, X, y, groups, parameter_iterable)
        477                 # `"param_%s" % name` at the first occurence of `name`.
        478                 # Setting the value at an index also unmasks that index
    --> 479                 param_results["param_%s" % name][cand_i] = value
        480 
        481         results.update(param_results)
    
    /opt/anaconda3/envs/py37/lib/python3.7/site-packages/sklearn/utils/deprecation.py in wrapped(*args, **kwargs)
        66         def wrapped(*args, **kwargs):
        67             warnings.warn(msg, category=FutureWarning)
    ---> 68             return init(*args, **kwargs)
        69         cls.__init__ = wrapped
        70 
    
    TypeError: object.__init__() takes exactly one argument (the instance to initialize)
    

    If I downgrade scikit-learn back to 0.23.0 and do the monkey patch as described above, everything works ok.

  5. holgern commented on May 20, 2020

    @holgern
    Contributor

    There are also changes in Gaussian processes regression in sklearn 0.23 which needs to be migrated:
    scikit-learn/scikit-learn#15782

  6. GuillemGSubies commented on Jun 5, 2020

    @GuillemGSubies

    Any updates on this? Can I help in any way?

  7. xmatthias commented on Jun 5, 2020

    @xmatthias
    Contributor

    scikit-learn==0.23.1 does solve this for now.
    they did properly deprecate the failing option now instead of simply removing:
    source: scikit-learn/scikit-learn#17199

  8. GuillemGSubies commented on Jun 5, 2020

    @GuillemGSubies

    But the error TypeError: object.__init__() takes exactly one argument (the instance to initialize) Is not solved with that, is it?

  9. xmatthias commented on Jun 5, 2020

    @xmatthias
    Contributor

    but that's not what this issue is about originally, that was just attached to this issue.

  10. magic-lantern commented on Jun 5, 2020

    @magic-lantern

    I added the information about TypeError: object.__init__() takes exactly one argument as it is part of scikit-optimize not being compatible with the most recent scikit-learn. Additionally some comments above said that the newest scikit-learn should remove the need for monkey patching. If it is preferred I can open a new issue (if the issue still exists).

  11. amueller commented on Jun 15, 2020

    @amueller
    Contributor

    I think this is actually part of the original issue as it was meant to be a fix, but I think there's an issue with the fix. If you want to look into it, I think we should fix it in sklearn.

  12. ZhaoJunLee commented on Jul 3, 2020

    @ZhaoJunLee

    Instead of
    from skopt import BayesSearchCV

    try,
    from skopt.searchcv import BayesSearchCV

    this works well for me.

  13. epx-dan commented on Jul 6, 2020

    @epx-dan

    I am using scikit-survival for building CoxPH survival model and BayesSearchCV for tunning, however, I have error TypeError: object.__init__() takes no parameters when I try to run BayesSearchCV(estimator=CoxPHSurvivalAnalysis(), search_spaces=params_coxph, cv=10, n_jobs=-1).fit(X_train, y_train). How to solve this?

    scikit-learn==0.23.1
    scikit-optimize==0.7.4
    scikit-survival==0.13.0

  14. holgern commented on Sep 4, 2020

    @holgern
    Contributor

    Version incompatibility has been fixed in #943 and #939 and in release 0.8.1

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions