skopt BayesSearchCV using deprecated np.int

58 Views Asked by At

I found out that skopt is using an np.int as a parameter in site-packages\skopt\space\transformers.py By looking at the error message, and reading this question which faces same issue as me. How to fix the 'numpy.int' attribute error when using skopt.BayesSearchCV in scikit-learn?

But np.int is deprecated in latest numpy

in the post above it is suggested that i could use np.arange(1,100) instead of Integer(1, 1000) but it gives the same error. Another way is to replace all np.int with int in the file '\skopt\space\transformers.py' I would rather not do that, as i am not using a virtual enviroment in my jupyter notebook.

What can i do?

from skopt import BayesSearchCV
from skopt.space import Real, Integer, Categorical
from skopt.plots import plot_objective, plot_histogram
from skopt.utils import use_named_args
from skopt.plots import plot_evaluations
from skopt import gp_minimize
from skopt.plots import plot_convergence

from sklearn.datasets import load_diabetes
from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import Pipeline

import seaborn as sns
import pandas as pd
import numpy as np
     

diabetes_data = load_diabetes()
diabetes_df = pd.DataFrame(data=np.c_[diabetes_data['data'], diabetes_data['target']], 
                           columns=diabetes_data['feature_names'] + ['target'])
diabetes_df
X_train, X_test, y_train, y_test = train_test_split(diabetes_df.drop(columns="target"), diabetes_df.target, test_size=0.2, random_state=21)
pipe = Pipeline([
                 ('model', GradientBoostingRegressor())
])

ada_search = {
    'model': [AdaBoostRegressor()],
    'model__learning_rate': Real(0.005, 0.9, prior="log-uniform"),
    'model__n_estimators': Integer(1, 1000),
    'model__loss': Categorical(['linear', 'square', 'exponential'])
}

gb_search = {
    'model': [GradientBoostingRegressor()],
    'model__learning_rate': Real(0.005, 0.9, prior="log-uniform"),
    'model__n_estimators': Integer(1, 1000),
    'model__loss': Categorical(['ls', 'lad', 'quantile'])
}

opt = BayesSearchCV(
    pipe,
    [(ada_search, 100), (gb_search, 100)],
    cv=5
)

opt.fit(X_train, y_train)     

Error:


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[12], line 25
     12 gb_search = {
     13     'model': [GradientBoostingRegressor()],
     14     'model__learning_rate': Real(0.005, 0.9, prior="log-uniform"),
     15     'model__n_estimators': Integer(1, 1000),
     16     'model__loss': Categorical(['ls', 'lad', 'quantile'])
     17 }
     19 opt = BayesSearchCV(
     20     pipe,
     21     [(ada_search, 100), (gb_search, 100)],
     22     cv=5
     23 )
---> 25 opt.fit(X_train, y_train)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\searchcv.py:466, in BayesSearchCV.fit(self, X, y, groups, callback, **fit_params)
    463 else:
    464     self.optimizer_kwargs_ = dict(self.optimizer_kwargs)
--> 466 super().fit(X=X, y=y, groups=groups, **fit_params)
    468 # BaseSearchCV never ranked train scores,
    469 # but apparently we used to ship this (back-compat)
    470 if self.return_train_score:

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\base.py:1152, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   1145     estimator._validate_params()
   1147 with config_context(
   1148     skip_parameter_validation=(
   1149         prefer_skip_nested_validation or global_skip_validation
   1150     )
   1151 ):
-> 1152     return fit_method(estimator, *args, **kwargs)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\model_selection\_search.py:898, in BaseSearchCV.fit(self, X, y, groups, **fit_params)
    892     results = self._format_results(
    893         all_candidate_params, n_splits, all_out, all_more_results
    894     )
    896     return results
--> 898 self._run_search(evaluate_candidates)
    900 # multimetric is determined here because in the case of a callable
    901 # self.scoring the return type is only known after calling
    902 first_test_score = all_out[0]["test_scores"]

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\searchcv.py:512, in BayesSearchCV._run_search(self, evaluate_candidates)
    508 while n_iter > 0:
    509     # when n_iter < n_points points left for evaluation
    510     n_points_adjusted = min(n_iter, n_points)
--> 512     optim_result = self._step(
    513         search_space, optimizer,
    514         evaluate_candidates, n_points=n_points_adjusted
    515     )
    516     n_iter -= n_points
    518     if eval_callbacks(callbacks, optim_result):

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\searchcv.py:400, in BayesSearchCV._step(self, search_space, optimizer, evaluate_candidates, n_points)
    397 """Generate n_jobs parameters and evaluate them in parallel.
    398 """
    399 # get parameter values to evaluate
--> 400 params = optimizer.ask(n_points=n_points)
    402 # convert parameters to python native types
    403 params = [[np.array(v).item() for v in p] for p in params]

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\optimizer\optimizer.py:395, in Optimizer.ask(self, n_points, strategy)
    393 X = []
    394 for i in range(n_points):
--> 395     x = opt.ask()
    396     X.append(x)
    398     ti_available = "ps" in self.acq_func and len(opt.yi) > 0

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\optimizer\optimizer.py:367, in Optimizer.ask(self, n_points, strategy)
    336 """Query point or multiple points at which objective should be evaluated.
    337 
    338 n_points : int or None, default: None
   (...)
    364 
    365 """
    366 if n_points is None:
--> 367     return self._ask()
    369 supported_strategies = ["cl_min", "cl_mean", "cl_max"]
    371 if not (isinstance(n_points, int) and n_points > 0):

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\optimizer\optimizer.py:434, in Optimizer._ask(self)
    430 if self._n_initial_points > 0 or self.base_estimator_ is None:
    431     # this will not make a copy of `self.rng` and hence keep advancing
    432     # our random state.
    433     if self._initial_samples is None:
--> 434         return self.space.rvs(random_state=self.rng)[0]
    435     else:
    436         # The samples are evaluated starting form initial_samples[0]
    437         return self._initial_samples[
    438             len(self._initial_samples) - self._n_initial_points]

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\space\space.py:900, in Space.rvs(self, n_samples, random_state)
    897 columns = []
    899 for dim in self.dimensions:
--> 900     columns.append(dim.rvs(n_samples=n_samples, random_state=rng))
    902 # Transpose
    903 return _transpose_list_array(columns)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\space\space.py:698, in Categorical.rvs(self, n_samples, random_state)
    696     return self.inverse_transform([(choices)])
    697 elif self.transform_ == "normalize":
--> 698     return self.inverse_transform(list(choices))
    699 else:
    700     return [self.categories[c] for c in choices]

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\space\space.py:685, in Categorical.inverse_transform(self, Xt)
    680 """Inverse transform samples from the warped space back into the
    681    original space.
    682 """
    683 # The concatenation of all transformed dimensions makes Xt to be
    684 # of type float, hence the required cast back to int.
--> 685 inv_transform = super(Categorical, self).inverse_transform(Xt)
    686 if isinstance(inv_transform, list):
    687     inv_transform = np.array(inv_transform)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\space\space.py:168, in Dimension.inverse_transform(self, Xt)
    164 def inverse_transform(self, Xt):
    165     """Inverse transform samples from the warped space back into the
    166        original space.
    167     """
--> 168     return self.transformer.inverse_transform(Xt)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\space\transformers.py:309, in Pipeline.inverse_transform(self, X)
    307 def inverse_transform(self, X):
    308     for transformer in self.transformers[::-1]:
--> 309         X = transformer.inverse_transform(X)
    310     return X

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\skopt\space\transformers.py:275, in Normalize.inverse_transform(self, X)
    273 X_orig = X * (self.high - self.low) + self.low
    274 if self.is_int:
--> 275     return np.round(X_orig).astype(np.int)
    276 return X_orig

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\__init__.py:338, in __getattr__(attr)
    333     warnings.warn(
    334         f"In the future `np.{attr}` will be defined as the "
    335         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    337 if attr in __former_attrs__:
--> 338     raise AttributeError(__former_attrs__[attr])
    340 if attr == 'testing':
    341     import numpy.testing as testing

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
0

There are 0 best solutions below