`scikit-optimize` 包中的 TypeError

TypeError inside the `scikit-optimize` package

当我使用 scikit-optimize 版本 0.7.4 优化 scikit-learn 0.23 模型时:

    rf = BayesSearchCV(
        RandomForestClassifier(
            min_samples_leaf=0.01, oob_score=True
        ), {
            'n_estimators': Integer(30, 200),
            'max_depth': Integer(10, 150),
            'min_samples_split': Real(0.05, 0.3),
        }, n_iter=32
    )

当我 运行 rf.fit 时,它说,

  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\space\space.py", line 764, in rvs
    if sp_version < (0, 16):
TypeError: '<' not supported between instances of 'Version' and 'tuple'

但是当我简单地使用RandomForestClassifier并拟合它时,不会出现错误。 那么,如何避免这个问题呢?谢谢!

完整的回溯如下

Traceback (most recent call last):
  File "C:/Users/cloudy/PyCharmProjects/clixove/BasicML/classifier.py", line 106, in <module>
    rf.fit(clf.data['X_train'], clf.data['Y_train'])
  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\searchcv.py", line 678, in fit
    optim_result = self._step(
  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\searchcv.py", line 552, in _step
    params = optimizer.ask(n_points=n_points)
  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\optimizer\optimizer.py", line 360, in ask
    x = opt.ask()
  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\optimizer\optimizer.py", line 332, in ask
    return self._ask()
  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\optimizer\optimizer.py", line 398, in _ask
    return self.space.rvs(random_state=self.rng)[0]
  File "C:\Users\cloudy\PyCharmProjects\clixove\venv\lib\site-packages\skopt\space\space.py", line 764, in rvs
    if sp_version < (0, 16):
TypeError: '<' not supported between instances of 'Version' and 'tuple'

我遇到了同样的问题。看起来 sci-kit 学习的新变化改变了版本的读取方式。检查更改 here.

如果未安装 setuptools,则使用 LooseVersion,returns 是 Version 类型而不是元组。

在您的 pip install setuptools 环境中安装 setuptools 可以解决这个问题吗?

如果 scikit-learn 版本对您的问题不重要,您可以通过 pip install -U scikit-learn == 0.20.3

将 scikit-learn 版本降级为 '0.20.3'

我已经解决了更改 skopt/space/space.py 行 763-768

 for dim in self.dimensions:
            
            if sp_version < (0, 16):
                columns.append(dim.rvs(n_samples=n_samples))
            else:
                columns.append(dim.rvs(n_samples=n_samples, random_state=rng))

进入

 for dim in self.dimensions:
            
            try:
                columns.append(dim.rvs(n_samples=n_samples, random_state=rng))
            except:
                columns.append(dim.rvs(n_samples=n_samples))

scikit-learn >= 0.23 的问题已在 0.8.1 版本中修复

PIP安装如下:

pip install scikit-optimize==0.8.1

参考:scikit-optimize 0.8.1