3

当我使用scikit-optimize0.7.4 版本优化scikit-learn0.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'
4

4 回答 4

3

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

以下是 PIP 安装:

pip install scikit-optimize==0.8.1

参考:scikit-optimize 0.8.1

于 2020-09-04T09:50:34.647 回答
1

我已经解决了更改 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))
于 2020-08-15T10:31:16.567 回答
1

如果 scikit-learn 版本在您的问题中不重要,您可以通过以下方式将 scikit-learn 版本降级为“0.20.3”pip install -U scikit-learn == 0.20.3

于 2020-08-14T19:38:15.873 回答
0

我遇到了同样的问题。看起来 sci-kit learn 的一个新变化改变了版本的读取方式。在此处查看更改。

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

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

于 2020-08-14T16:06:14.453 回答