How to fix the 'numpy.int' attribute error when using skopt.BayesSearchCV in scikit-learn?

6.2k Views Asked by At

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?

4

There are 4 best solutions below

0
On

I managed to resolve this by simply adding this line before calling .fit()

opt = BayesSearchCV(...)

np.int = int
opt.fit(...)

Hope this helps.

0
On

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.
2
On

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

1
On

`

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

param_space = {
    'n_estimators': [100, 200, 300, 400, 500],
    'learning_rate': [0.01, 0.05, 0.1, 0.3, 0.5],
    'max_depth': [3, 5, 7, 9, 11],
    'reg_alpha': [0.1, 0.3, 0.5],
    'reg_lambda': [0.1, 0.3, 0.5]
}
# Create the XGBoost model
model = XGBClassifier()

bayes_search = BayesSearchCV(
    estimator=model,
    search_spaces=param_space,
    scoring='f1_macro',
    cv=3,
    n_jobs=-1,
    n_iter=50,  
    random_state=42
)
# added this line to my code
np.int = int

bayes_search.fit(X_train, y_train)

` JUST ADD THE ABOVE LINE TO THE CODE IT WORKS