在使用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