在 scikit-learn 中使用 skopt.BayesSearchCV 时如何修复“numpy.int”属性错误?

san*_*tus 8 python machine-learning scikit-learn scikit-optimize bayessearchcv

当我在官方文档上运行以下代码时,出现错误。

最小的例子

from skopt import BayesSearchCV
from sklearn.datasets import load_digits
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

X, y = load_digits(n_class=10, return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, test_size=.25, random_state=0)

# log-uniform: understand as search over p = exp(x) by varying x
opt = BayesSearchCV(
    SVC(),
    {
        'C': (1e-6, 1e+6, 'log-uniform'),
        'gamma': (1e-6, 1e+1, 'log-uniform'),
        'degree': (1, 8),  # integer valued parameter
        'kernel': ['linear', 'poly', 'rbf'],  # categorical parameter
    },
    n_iter=32,
    cv=3
)

opt.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)

最后一行产生错误:

AttributeError 模块“numpy”没有属性“int”。 np.int是内置的已弃用的别名int。为了避免现有代码中出现此错误,请int单独使用。这样做不会改变任何行为并且是安全的。替换 时np.int,您可能希望使用 例如np.int64np.int32来指定精度。如果您想查看当前的使用情况,请查看发行说明链接以获取更多信息。别名最初在 NumPy 1.20 中已弃用;有关更多详细信息和指导,请参阅原始发行说明: https ://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

我怎么解决这个问题?还有其他方法来实现贝叶斯搜索吗?

也许skopt的版本太旧了。还有其他方法来实现贝叶斯搜索吗?除了网格搜索、随机搜索和贝叶斯搜索之外,还有其他方法可以帮助我选择机器学习模型的超参数吗?

Nar*_*yan 8

我通过在调用 .fit() 之前添加这一行来解决这个问题

opt = BayesSearchCV(...)

np.int = int
opt.fit(...)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。


Lea*_*ess 5

目前尚不清楚哪一行代码引发了异常,但我怀疑罪魁祸首是 skopt 没有赶上np.intnumpy 的完全弃用。您可以将 numpy 降级为<1.24或者此更改可以做到:

        'degree': (1, 8),  # integer valued parameter
Run Code Online (Sandbox Code Playgroud)

        'degree': np.arange(1,9),
Run Code Online (Sandbox Code Playgroud)

将整数维度作为整数数组传递。

编辑:我发现这个正在进行的MR可以解决这个问题

  • 事实上,正是“opt.fit()”行引发了错误。更准确地说,“BayesSearchCV.fit()”使用“np.int”。我根据您提供的链接将文件“site-packages\skopt\space\transformers.py”中的所有“np.int”替换为“int”。该代码似乎运行良好。多谢。 (6认同)