由于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))
最佳答案
对于我来说有效的方法(带有有关原因的链接),作为针对此问题的解决方法以及_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类即可。