Hyperopt报错HyperoptEstimator.fit报错[Errno 22] Invalid argument的解决办法

在使用Hyperopt测试下面官方的例子时,报错[Errno 22] Invalid argument

from hpsklearn import HyperoptEstimator, any_classifier,any_preprocessing
from sklearn.datasets import load_iris
from hyperopt import tpe
import numpy as np

# Download the data and split into training and test sets
iris = load_iris()
X = iris.data
y = iris.target

test_size = int(0.2 * len(y))
np.random.seed(13)
indices = np.random.permutation(len(X))
X_train = X[indices[:-test_size]]
y_train = y[indices[:-test_size]]
X_test = X[indices[-test_size:]]
y_test = y[indices[-test_size:]]

# Instantiate a HyperoptEstimator with the search space and number of evaluations
estim = HyperoptEstimator(classifier=any_classifier('my_clf'),
                          preprocessing=any_preprocessing('my_pre'),
                          algo=tpe.suggest)

# Search the hyperparameter space based on the data
estim.fit(X_train, y_train)

# Show the results
print estim.score(X_test, y_test) 

报错详细内容,

Traceback (most recent call last):
  File "<ipython-input-11-8f964d113a8b>", line 1, in <module>
    runfile('C:/Users/asus/untitled56.py', wdir='C:/Users/asus')
  File "d:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "d:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/asus/untitled56.py", line 29, in <module>
    estim.fit(X_train, y_train)
  File "d:\Program Files\Anaconda2\lib\site-packages\hpsklearn\estimator.py", line 746, in fit
    fit_iter.send(increment)
  File "d:\Program Files\Anaconda2\lib\site-packages\hpsklearn\estimator.py", line 657, in fit_iter
    return_argmin=False, # -- in case no success so far
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 367, in fmin
    return_argmin=return_argmin,
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\base.py", line 635, in fmin
    return_argmin=return_argmin)
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 385, in fmin
    rval.exhaust()
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 244, in exhaust
    self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 218, in run
    self.serial_evaluate()
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\fmin.py", line 137, in serial_evaluate
    result = self.domain.evaluate(spec, ctrl)
  File "d:\Program Files\Anaconda2\lib\site-packages\hyperopt\base.py", line 840, in evaluate
    rval = self.fn(pyll_rval)
  File "d:\Program Files\Anaconda2\lib\site-packages\hpsklearn\estimator.py", line 605, in fn_with_timeout
    th.start()
  File "d:\Program Files\Anaconda2\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "d:\Program Files\Anaconda2\lib\multiprocessing\forking.py", line 280, in __init__
    to_child.close()
IOError: [Errno 22] Invalid argument

最后找到了解决方法,只需加上if __name__ == '__main__':即可,具体如下。

from hpsklearn import HyperoptEstimator, any_classifier,any_preprocessing
from sklearn.datasets import load_iris
from hyperopt import tpe
import numpy as np

# Download the data and split into training and test sets
iris = load_iris()
X = iris.data
y = iris.target

test_size = int(0.2 * len(y))
np.random.seed(13)
indices = np.random.permutation(len(X))
X_train = X[indices[:-test_size]]
y_train = y[indices[:-test_size]]
X_test = X[indices[-test_size:]]
y_test = y[indices[-test_size:]]

# Instantiate a HyperoptEstimator with the search space and number of evaluations
estim = HyperoptEstimator(classifier=any_classifier('my_clf'),
                          preprocessing=any_preprocessing('my_pre'),
                          algo=tpe.suggest,
                          max_evals=100,
                          trial_timeout=120)

if __name__ == '__main__':
    # Search the hyperparameter space based on the data
    estim.fit(X_train, y_train)
    
    # Show the results
    print estim.score(X_test, y_test) 

 输出结果:0.933333333333

完美解决!

参考:https://github.com/hyperopt/hyperopt-sklearn/issues/72

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值