2

When I run the following code on the official documentation, it has an error.

Minimal example

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)

The last line produces an error:

AttributeError module 'numpy' has no attribute 'int'. np.int was a deprecated alias for the builtin int. To avoid this error in existing code, use int by itself. Doing this will not modify any behavior and is safe. When replacing np.int, you may wish to use e.g. np.int64 or np.int32 to specify the precision. If you wish to review your current use, check the release note link for additional information. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

How can I solve this problem? Are there other ways to implement Bayesian search?

Perhaps the version of skopt is too old. Are there other ways to implement Bayesian search? Besides grid search, random search, and bayes search, is there any other way to help me choose the hyperparameters of the machine learning model?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
sanctus
  • 21
  • 1

2 Answers2

2

It's not clear which line of code is raising the exception but I suspect the culprit is skopt not having caught up with the full deprecation of np.int in numpy. You could downgrade numpy to <1.24 or this change could do: from

        'degree': (1, 8),  # integer valued parameter

to

        'degree': np.arange(1,9),

to pass the integer dimension as a array of integer.

Edit: I found this ongoing MR to fix the issue

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • 4
    In fact it's the line `opt.fit()` that raises the error. More precisely, `BayesSearchCV.fit()` uses `np.int`. I replaced all `np.int` with `int` in the file 'site-packages\skopt\space\transformers.py' according to the link you provided. The code seems to be working well. thanks a lot. – sanctus May 24 '23 at 13:04
  • 1
    Arrrf, I wished you did not have to update the package itself but... whatever works! skopt has not been updated since late 2021 looks like it won't get the fix any time soon. Glad I could help. You may want to post your fix as an answer and validate asap – Learning is a mess May 24 '23 at 13:06
0

I got the same error while trying to fit BayesSearchCV

BayesSearchCV(
            SVR(),
            param_distributions,
            n_iter,
            cv,
            scoring,
            random_state,
            n_jobs).fit(X_train, y_train). 
 

AttributeError: module 'numpy' has no attribute 'int'.

As @sanctus suggested:

  1. Replaced all np.int with int in the file 'anaconda3\envs\myenv\Lib\site-packages\skopt\space\transformers.py'
  2. Restarted kernel, or in my case exited pycharm app and opened again
  3. run the code again and error fixed.
prgrmmer
  • 1
  • 1