BayesSearchCV无法正常工作,原因是fit_params

时间:2019-06-15 11:01:39

标签: python scikit-learn

由于fit_params而无法正常工作

https://scikit-optimize.github.io/

此代码是上述URL的代码。

但是它不起作用。 我们该如何解决呢?

from skopt import BayesSearchCV
from skopt.space import Real, Categorical, Integer

from sklearn.datasets import load_iris 
from sklearn.svm import SVC 
from sklearn.model_selection import train_test_split

X, y = load_iris(True) 
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, random_state=0)

opt = BayesSearchCV( SVC(), { 'C': Real(1e-6, 1e+6, prior='log-uniform'), 'gamma': Real(1e-6, 1e+1, prior='log-uniform'), 'degree': Integer(1,8), 'kernel': Categorical(['linear', 'poly', 'rbf']), },
                    n_iter=32 )
opt.fit(X_train, y_train)
print(opt.score(X_test, y_test))

earn

2 个答案:

答案 0 :(得分:1)

针对此问题,请尝试修复:

pip install git+https://github.com/darenr/scikit-optimize

它为我工作,并让我继续前进。

答案 1 :(得分:0)

对于我来说有效的方法(带有相关原因的链接),作为解决此问题以及_run_search错误的方法:

class FixedBayesSearchCV(BayesSearchCV):
"""
Dirty hack to avoid compatibility issues with sklearn 0.2 and skopt.
Credit: https://www.kaggle.com/c/home-credit-default-risk/discussion/64004

For context, on why the workaround see:
    - https://github.com/scikit-optimize/scikit-optimize/issues/718
    - https://github.com/scikit-optimize/scikit-optimize/issues/762
"""
def __init__(self, estimator, search_spaces, optimizer_kwargs=None,
            n_iter=50, scoring=None, fit_params=None, n_jobs=1,
            n_points=1, iid=True, refit=True, cv=None, verbose=0,
            pre_dispatch='2*n_jobs', random_state=None,
            error_score='raise', return_train_score=False):
    """
    See: https://github.com/scikit-optimize/scikit-optimize/issues/762#issuecomment-493689266
    """

    # Bug fix: Added this line
    self.fit_params = fit_params

    self.search_spaces = search_spaces
    self.n_iter = n_iter
    self.n_points = n_points
    self.random_state = random_state
    self.optimizer_kwargs = optimizer_kwargs
    self._check_search_space(self.search_spaces)

    # Removed the passing of fit_params to the parent class.
    super(BayesSearchCV, self).__init__(
            estimator=estimator, scoring=scoring, n_jobs=n_jobs, iid=iid,
            refit=refit, cv=cv, verbose=verbose, pre_dispatch=pre_dispatch,
            error_score=error_score, return_train_score=return_train_score)

def _run_search(self, x):
    raise BaseException('Use newer skopt')

只需像使用BayesSearchCV一样使用此FixedBayesSearchCV类。