简体   繁体   English

`scikit-optimize` package 中的 TypeError

[英]TypeError inside the `scikit-optimize` package

When I use scikit-optimize version 0.7.4 to optimize a scikit-learn 0.23 model:当我使用scikit-optimize版本 0.7.4 优化scikit-learn 0.23 model 时:

    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
    )

When I run rf.fit , it says,当我运行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'

But when I simply use RandomForestClassifier , and fit it, the error doesn't occur.但是当我简单地使用RandomForestClassifier并适合它时,不会发生错误。 So, how to avoid this problem?那么,如何避免这个问题呢? Thank you!谢谢!

The full of traceback is as following.完整的回溯如下。

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'

If scikit-learn version is not important in your problem, you can downgrade scikit-learn version to '0.20.3' by pip install -U scikit-learn == 0.20.3如果 scikit-learn 版本在您的问题中不重要,您可以通过pip install -U scikit-learn == 0.20.3版本降级为 '0.20.3'

I've solved changing skopt/space/space.py lines 763-768我已经解决了更改 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))

into进入

 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))

The problems with scikit-learn >= 0.23 have been fixed in version 0.8.1 scikit-learn >= 0.23 的问题已在 0.8.1 版本中修复

Following is the PIP installation:以下是 PIP 安装:

pip install scikit-optimize==0.8.1

Reference: scikit-optimize 0.8.1参考: scikit-optimize 0.8.1

I encountered the same issue.我遇到了同样的问题。 It looks like a new change to sci-kit learn changed how versions are read.看起来 sci-kit learn 的一个新变化改变了版本的读取方式。 Checkout the change here .此处查看更改。

If setuptools is not installed, LooseVersion is used, which returns a Version type rather than a tuple.如果未安装setuptools ,则使用LooseVersion ,它返回Version类型而不是元组。

Does installing setuptools in your environment with pip install setuptools solve this for you?使用pip install setuptools setuptools在您的环境中安装 setuptools 是否可以为您解决这个问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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