首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于fit_params,BayesSearchCV无法工作

由于fit_params,BayesSearchCV无法工作
EN

Stack Overflow用户
提问于 2019-06-15 19:01:39
回答 2查看 1.9K关注 0票数 1

由于fit_params而无法工作

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

这个代码就是上面URL的代码。

但它不起作用。我们怎么解决这个问题呢?

代码语言:javascript
复制
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))

EN

回答 2

Stack Overflow用户

发布于 2019-07-10 21:21:50

一些对我有效的东西(有相关来源的链接说明原因)作为这个问题和_run_search错误的变通方法:

代码语言:javascript
复制
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类。

票数 2
EN

Stack Overflow用户

发布于 2019-11-03 19:13:28

对于此问题,请尝试修复程序:

代码语言:javascript
复制
pip install git+https://github.com/darenr/scikit-optimize

它为我工作,并允许我继续下去。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56609726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档