我尝试使用RF分类器,但每次尝试运行bayessearchCV函数时都会返回错误。附件中是我具体的示例和一个您可以运行并复现的示例。我怀疑这可能是由于train_test_split函数引起的,但我并不完全确定如何排查。请告诉我我的代码中是否有明显的错误…
我目前使用的是最新的sklearn/skopt/numpy等版本
import numpy as npimport pandas as pdfrom sklearn import preprocessingfrom matplotlib import pyplot as pltimport xgboost as xgbimport sklearnfrom sklearn.linear_model import LogisticRegressionfrom sklearn.linear_model import ElasticNetfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score from sklearn.metrics import roc_auc_scorefrom skopt import BayesSearchCVfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.model_selection import GridSearchCVopt = BayesSearchCV( RandomForestClassifier(random_state=42), { 'n_estimators': (5,5000), 'max_features': ['auto','sqrt'], 'max_depth': (2,90), 'min_samples_split': (2,10), 'min_samples_leaf': (1,7), 'bootstrap': ["True","False"] }, n_iter=32, cv=3, scoring='roc_auc')opt.fit(full_train, full_y_train)print("val. score: %s" % opt.best_score_)print("test score: %s" % opt.score(X_test_red, y_test))
错误
/Users/user/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/deprecation.py:67: FutureWarning: Class MaskedArray is deprecated; MaskedArray is deprecated in version 0.23 and will be removed in version 0.25. Use numpy.ma.MaskedArray instead. warnings.warn(msg, category=FutureWarning)---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-20-8b1596e90c35> in <module>----> 1 opt.fit(full_train, full_y_train) 2 3 print("val. score: %s" % opt.best_score_) 4 print("test score: %s" % opt.score(X_test_red, y_test))~/opt/anaconda3/lib/python3.8/site-packages/skopt/searchcv.py in fit(self, X, y, groups, callback)~/opt/anaconda3/lib/python3.8/site-packages/skopt/searchcv.py in _step(self, X, y, search_space, optimizer, groups, n_points)~/opt/anaconda3/lib/python3.8/site-packages/skopt/searchcv.py in _fit(self, X, y, groups, parameter_iterable)~/opt/anaconda3/lib/python3.8/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)
供您复现的示例
from skopt import BayesSearchCVfrom sklearn.datasets import load_digitsfrom sklearn.svm import SVCfrom sklearn.model_selection import train_test_splitX, 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 xopt = 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))
在我的机器上,这会产生与第一个示例相同的错误。
回答:
sklearn >= 0.23.0 的问题已在skopt版本0.8.1中修复。https://pypi.org/project/scikit-optimize/0.8.1/