我正在尝试使用 Hyperopt 使用 Google Colab 优化我的数据集上的分类任务。但是,它的实用程序之一,交叉验证不起作用并引发此错误:TypeError: init () got an unexpected keyword argument 'n_iter'。另外,即使我从代码中删除了交叉验证参数,有时它仍然会给出相同的错误,并且我必须多次重新运行相同的代码才能达到没有任何错误的结果。我在互联网上搜索,这里显示的实现对我不起作用。我的代码和引发的错误如下:
from sklearn.model_selection import KFold, cross_val_score
from hpsklearn import HyperoptEstimator, any_classifier
from sklearn.model_selection import train_test_split
from hyperopt import tpe, hp, fmin, Trials
import numpy as np
type(aust_predictors)
X_train, X_test, y_train, y_test = train_test_split(aust_predictors, df_aust.target, train_size = 0.75, test_size = 0.25)
y_train = y_train.to_numpy()
y_test = y_test.to_numpy()
estim = HyperoptEstimator(classifier=any_classifier('clf'), algo = tpe.suggest, trial_timeout=1500)
estim.fit(X_train, y_train)
print(estim.score(X_test, y_test))
print(estim.best_model())
cv_sc = cross_val_score(estim, X_test, y_test, cv = 10, n_jobs=-1).mean()
print(cv_sc)
100%|██████████| 1/1 [00:00<00:00, 1.50it/s, best loss: 0.15384615384615385]
100%|██████████| 1/1 [00:00<00:00, 9.24it/s, best loss: 0.09615384615384615]
100%|██████████| 1/1 [00:00<00:00, 2.91it/s, best loss: 0.09615384615384615]
100%|██████████| 1/1 [00:04<00:00, 4.48s/it, best loss: 0.09615384615384615]
0%| | 0/1 [00:00<?, ?it/s, best loss: ?]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-8010315da9aa> in <module>()
1 estim = HyperoptEstimator(classifier=any_classifier('clf'), algo = tpe.suggest, trial_timeout=1500)
----> 2 estim.fit(X_train, y_train)
3
4 print(estim.score(X_test, y_test))
5 print(estim.best_model())
10 frames
/usr/local/lib/python3.6/dist-packages/hpsklearn/estimator.py in fit(self, X, y, EX_list, valid_size, n_folds, cv_shuffle, warm_start, random_state, weights)
744 increment = min(self.fit_increment,
745 adjusted_max_evals - len(self.trials.trials))
--> 746 fit_iter.send(increment)
747 if filename is not None:
748 with open(filename, 'wb') as dump_file:
/usr/local/lib/python3.6/dist-packages/hpsklearn/estimator.py in fit_iter(self, X, y, EX_list, valid_size, n_folds, cv_shuffle, warm_start, random_state, weights, increment)
655 # so we notice them.
656 catch_eval_exceptions=False,
--> 657 return_argmin=False, # -- in case no success so far
658 )
659 else:
/usr/local/lib/python3.6/dist-packages/hyperopt/fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar)
386 catch_eval_exceptions=catch_eval_exceptions,
387 return_argmin=return_argmin,
--> 388 show_progressbar=show_progressbar,
389 )
390
/usr/local/lib/python3.6/dist-packages/hyperopt/base.py in fmin(self, fn, space, algo, max_evals, rstate, verbose, pass_expr_memo_ctrl, catch_eval_exceptions, return_argmin, show_progressbar)
637 catch_eval_exceptions=catch_eval_exceptions,
638 return_argmin=return_argmin,
--> 639 show_progressbar=show_progressbar)
640
641
/usr/local/lib/python3.6/dist-packages/hyperopt/fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar)
405 show_progressbar=show_progressbar)
406 rval.catch_eval_exceptions = catch_eval_exceptions
--> 407 rval.exhaust()
408 if return_argmin:
409 return trials.argmin
/usr/local/lib/python3.6/dist-packages/hyperopt/fmin.py in exhaust(self)
260 def exhaust(self):
261 n_done = len(self.trials)
--> 262 self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
263 self.trials.refresh()
264 return self
/usr/local/lib/python3.6/dist-packages/hyperopt/fmin.py in run(self, N, block_until_done)
225 else:
226 # -- loop over trials and do the jobs directly
--> 227 self.serial_evaluate()
228
229 try:
/usr/local/lib/python3.6/dist-packages/hyperopt/fmin.py in serial_evaluate(self, N)
139 ctrl = base.Ctrl(self.trials, current_trial=trial)
140 try:
--> 141 result = self.domain.evaluate(spec, ctrl)
142 except Exception as e:
143 logger.info('job exception: %s' % str(e))
/usr/local/lib/python3.6/dist-packages/hyperopt/base.py in evaluate(self, config, ctrl, attach_attachments)
841 self.expr,
842 memo=memo,
--> 843 print_node_on_error=self.rec_eval_print_node_on_error)
844 rval = self.fn(pyll_rval)
845
/usr/local/lib/python3.6/dist-packages/hyperopt/pyll/base.py in rec_eval(expr, deepcopy_inputs, memo, max_program_len, memo_gc, print_trace, print_node_on_error)
911
912 try:
--> 913 rval = scope._impls[node.name](*args, **kwargs)
914
915 except Exception as e:
/usr/local/lib/python3.6/dist-packages/hpsklearn/components.py in sklearn_SGDClassifier(*args, **kwargs)
89 @scope.define
90 def sklearn_SGDClassifier(*args, **kwargs):
---> 91 return sklearn.linear_model.SGDClassifier(*args, **kwargs)
92
93 @scope.define
TypeError: __init__() got an unexpected keyword argument 'n_iter'
我确实尝试将 n_iter 从 None 更改为 0(这是我之前在 Github 中看到的推荐的东西),但它在 estimator.py 中已经是 0,如下所示:
n_iters = 0 # 跟踪训练迭代次数
所以我想不出任何其他的解决方案。我该如何解决这个问题?