简体   繁体   中英

Hyperopt classification task raises error "TypeError: __init__() got an unexpected keyword argument 'n_iter' "

I am trying to use Hyperopt to optimize a classification task on my dataset using Google Colab. However, one of its utilities, cross-validation does not work and raises this error : TypeError: init () got an unexpected keyword argument 'n_iter'. Also, even though I deleted the cross-validation parameter from the code, it still gives the same error sometimes, and I have to re-run the same code several times to reach a result without any error. I searched on the internet and the implementation that is shown here did not work for me. My code and the raised errors are below:

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'

I did try to change n_iter from None to 0 (which was a recommended thing in Github that I previously saw) but it was already 0 in estimator.py like this:

n_iters = 0 # Keep track of the number of training iterations

So I could not come up with any other solution. How can I solve this issue?

我遇到了同样的问题。安装最新版本解决了 colab 的问题。尝试使用下面显示的命令进行安装。请在重新开始工作之前重新启动运行时。

!pip install git+https://github.com/hyperopt/hyperopt-sklearn.git

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM