BayesianOptimization icon indicating copy to clipboard operation
BayesianOptimization copied to clipboard

Error with scipy 1.8.0

Open bwheelz36 opened this issue 3 years ago • 15 comments

I am getting an error with scipy 1.8.0

File "/home/brendan/python/TestVenv/lib/python3.8/site-packages/bayes_opt/util.py", line 65, in acq_max
if max_acq is None or -res.fun[0] >= max_acq:
TypeError: 'float' object is not subscriptable

The problem appears to be that in this new version of scipy, res.fun is returned as a number instead of an array. I haven't yet found exactly where in scipy the change is. A fix may be as simple as adding an if statement to handle this case.

I'll keep looking into it but for now I suggest limiting scipy installations to 1.7 or less.

bwheelz36 avatar Feb 07 '22 05:02 bwheelz36

I have the same problem. I used pip install scipy==1.7 and it seems to work.

jobquiroz avatar Feb 11 '22 03:02 jobquiroz

Came here with the same issue. pip install scipy==1.7 fixed it for me as well.

bfeif avatar Mar 01 '22 14:03 bfeif

Same, pip install scipy==1.7 did it for me

aeslava97 avatar Mar 08 '22 04:03 aeslava97

Glad to see others have reproduced this issue,

#319 should handle this in a version-explicit way.

Thanks!

ngupta20 avatar May 31 '22 02:05 ngupta20

hey all, just an update on this. I have permissions to manage this repository now and I have merged #303 into the main branch, which should resolve this error. However, I am still not able to release the code through pypi or conda forge. I will keep trying to get these permissions, but for now I would recommend installing directly from the master branch like this:

 pip install git+https://github.com/fmfn/BayesianOptimization

bwheelz36 avatar Jun 05 '22 23:06 bwheelz36

@bwheelz36 Any update on this after over a month? For a package with 6.1k stars, getting this fix pushed to PyPI/conda would seem to be quite critical.

peterdsharpe avatar Jul 21 '22 02:07 peterdsharpe

Agreed, the problem is I don't actually have the rights to do that yet. I'll try to get in touch with Fernando again though, thanks for the reminder.

bwheelz36 avatar Jul 21 '22 02:07 bwheelz36

Assuming I get the rights, I can handle the pypi release - does anyone want to volunteer to help with the conda release? I guess I could figure it out but I don't normally use conda...

bwheelz36 avatar Jul 21 '22 02:07 bwheelz36

As far as I am concerned, this has NOT fixed, after I downgraded scipy version to 1.70 and installed "pip install git+https://github.com/fmfn/BayesianOptimization" I still got the same error, can anyone explain why?

xxl4tomxu98 avatar Jul 27 '22 16:07 xxl4tomxu98

Should I only do ONE of the above, namely either downgrade Scipy OR install Master branch of the repo, not both?

xxl4tomxu98 avatar Jul 27 '22 16:07 xxl4tomxu98

@xxl4tomxu98 - either should fix it. if you install scipy version 1.8, install from the master branch here (which will also work with scipy 1.7) if you pip install, make sure you have scipy version <1.8. the most likely explanation as to why it didn't work when you downgraded to 1.7 is that somehow you hadn't actually downgraded with 1.7, or you did then it later got updated somehow. you could check:

import scipy
scipy.__version__

bwheelz36 avatar Jul 27 '22 16:07 bwheelz36

well, I do conda list and checked to see scipy is V1.7

xxl4tomxu98 avatar Jul 27 '22 21:07 xxl4tomxu98

My bayes_opt is also installed with conda

xxl4tomxu98 avatar Jul 27 '22 21:07 xxl4tomxu98

Assuming I get the rights, I can handle the pypi release - does anyone want to volunteer to help with the conda release? I guess I could figure it out but I don't normally use conda...

@bwheelz36 I also have no experience maintaining conda packages, but I'm eager for this change to be available through conda. So if you think it'd actually be helpful, I'd be happy to lend a hand (basically just working off https://conda-forge.org/docs/maintainer/updating_pkgs.html#updating-recipes). Other people would obviously be better equipped, but let me know if you just need another person to help sort through the steps.

daeh avatar Aug 17 '22 19:08 daeh

Hi @daeh Thanks! I'm not sure if you already read through #339 - basically the conda package should get updated once the pypi package gets updated, which should happen when I get the rights to it..

bwheelz36 avatar Aug 18 '22 04:08 bwheelz36

@bwheelz36, I'm still getting this error - I tried installing with pip, conda, and github, tried scipy==1.7 and 1.8, each on both python 3.10 and python 3.7. I see this was closed but am curious if this fix was uploaded?

In which environment does this work? Note I'm on Apple Silicon (M1 pro)

SETUP:

pbounds = {
    'n_estimators': (1, 50),
    'max_depth': (4, 40),
    'learning_rate': (0.3, 0.3),
    'colsample_bytree': (0.5, 1.),
    'min_child_weight': (1, 14),
    'subsample': (0.5, 1.),
    'reg_alpha': (0, 6),
    'reg_lambda': (1, 6),
    'gamma': (1, 6)
}

optimizer = BayesianOptimization(
    f=fit_bayesian,
    pbounds=pbounds,
    random_state=1
)

optimizer.maximize(
    init_points=2,
    n_iter=6,
)

ERROR:

/var/folders/96/g2vfz16s07jb12xnxmvl1zc80000gp/T/ipykernel_94169/80009563.py in <module>
      1 optimizer.maximize(
      2     init_points=2,
----> 3     n_iter=6,
      4 )

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/bayes_opt/bayesian_optimization.py in maximize(self, init_points, n_iter, acquisition_function, acq, kappa, kappa_decay, kappa_decay_delay, xi, **gp_params)
    309                 x_probe = self.suggest(util)
    310                 iteration += 1
--> 311             self.probe(x_probe, lazy=False)
    312 
    313             if self._bounds_transformer and iteration > 0:

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/bayes_opt/bayesian_optimization.py in probe(self, params, lazy)
    206             self._queue.add(params)
    207         else:
--> 208             self._space.probe(params)
    209             self.dispatch(Events.OPTIMIZATION_STEP)
    210 

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/bayes_opt/target_space.py in probe(self, params)
    234         x = self._as_array(params)
    235         params = dict(zip(self._keys, x))
--> 236         target = self.target_func(**params)
    237 
    238         if self._constraint is None:

/var/folders/96/g2vfz16s07jb12xnxmvl1zc80000gp/T/ipykernel_94169/129796447.py in fit_bayesian(n_estimators, max_depth, learning_rate, colsample_bytree, min_child_weight, subsample, reg_alpha, reg_lambda, gamma)
     39             reg_alpha=reg_alpha,
     40             reg_lambda=reg_lambda,
---> 41             gamma=gamma
     42             )
     43 

/var/folders/96/g2vfz16s07jb12xnxmvl1zc80000gp/T/ipykernel_94169/2885977117.py in fit_model(X_train, y_train, n_estimators, max_depth, learning_rate, colsample_bytree, min_child_weight, subsample, reg_alpha, reg_lambda, gamma)
     25             reg_lambda=reg_lambda,
     26             gamma=gamma)
---> 27         cv_model = model.fit(X_train, y_train)
     28 
     29         return cv_model

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/xgboost/core.py in inner_f(*args, **kwargs)
    573         for k, arg in zip(sig.parameters, args):
    574             kwargs[k] = arg
--> 575         return f(**kwargs)
    576 
    577     return inner_f

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/xgboost/sklearn.py in fit(self, X, y, sample_weight, base_margin, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, base_margin_eval_set, feature_weights, callbacks)
    970             verbose_eval=verbose,
    971             xgb_model=model,
--> 972             callbacks=callbacks,
    973         )
    974 

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/xgboost/core.py in inner_f(*args, **kwargs)
    573         for k, arg in zip(sig.parameters, args):
    574             kwargs[k] = arg
--> 575         return f(**kwargs)
    576 
    577     return inner_f

/opt/anaconda3/envs/bayes/lib/python3.7/site-packages/xgboost/training.py in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, xgb_model, callbacks, custom_metric)
    176     bst = cb_container.before_training(bst)
    177 
--> 178     for i in range(start_iteration, num_boost_round):
    179         if cb_container.before_iteration(bst, i, dtrain, evals):
    180             break

TypeError: 'numpy.float64' object cannot be interpreted as an integer

edubu2 avatar Jan 04 '23 20:01 edubu2

Hi, yes the fix has been uploaded to pypi. could you let me know which version of each package you are using by pasting the output of pip freeze in a terminal? Are you using a fresh environment? the most likely explanation is that a different package version than you think is being called.

bwheelz36 avatar Jan 05 '23 00:01 bwheelz36

I think that this is a different error, unrelated to this package. Either start_iteration or num_boost_round is a float/np.float64 when it should be of type int. Can you please post your fit_bayesian function, too?

till-m avatar Jan 05 '23 08:01 till-m