python - BayesSearchCV 由于 fit_params 而无法工作

标签 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

最佳答案

对我有用的东西(带有相关来源的链接来说明原因)作为此问题以及 _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 一样使用此 FixBayesSearchCV 类即可。

关于python - BayesSearchCV 由于 fit_params 而无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609726/

相关文章:

python - 使用有效字符在文件名中保存日期时间

python - 使用条件检查切换进程而不是在 wordLadder 中交换

python - 如何运行作为我在 AWS SSH session 中上传的应用程序的一部分的 Python 脚本?

python - 将表数据抓取到 .csv 中

python - 机器学习电子邮件优先级 - Python

python - python中的PLS-DA算法

python - 值错误 : y contains new labels: ['#' ]

python - 在二值图像上分割重叠的粗线

python - 使用 sklearn.cluster.KMeans (python + py2exe) 时减小 dist 目录大小

python - 为什么sklearn RandomForest模型保存后占用大量磁盘空间?