david-cortes / contextualbandits

Python implementations of contextual bandits algorithms
http://contextual-bandits.readthedocs.io
BSD 2-Clause "Simplified" License
750 stars 148 forks source link

Error Occurs when Running the Online Contextual Bandits Tutorial #33

Closed YanjieHe closed 3 years ago

YanjieHe commented 3 years ago

When I execute this code block:

from sklearn.linear_model import LogisticRegression
from contextualbandits.online import BootstrappedUCB, BootstrappedTS, LogisticUCB, \
            SeparateClassifiers, EpsilonGreedy, AdaptiveGreedy, ExploreFirst, \
            ActiveExplorer, SoftmaxExplorer
from copy import deepcopy

nchoices = y.shape[1]
base_algorithm = LogisticRegression(solver='lbfgs', warm_start=True)
beta_prior = ((3./nchoices, 4), 2) # until there are at least 2 observations of each class, will use this prior
beta_prior_ucb = ((5./nchoices, 4), 2) # UCB gives higher numbers, thus the higher positive prior
beta_prior_ts = ((2./np.log2(nchoices), 4), 2)
### Important!!! the default values for beta_prior will be changed in version 0.3

## The base algorithm is embedded in different metaheuristics
bootstrapped_ucb = BootstrappedUCB(deepcopy(base_algorithm), nchoices = nchoices,
                                   beta_prior = beta_prior_ucb, percentile = 80,
                                   random_state = 1111)
bootstrapped_ts = BootstrappedTS(deepcopy(base_algorithm), nchoices = nchoices,
                                 beta_prior = beta_prior_ts, random_state = 2222)
one_vs_rest = SeparateClassifiers(deepcopy(base_algorithm), nchoices = nchoices,
                                  beta_prior = beta_prior, random_state = 3333)
epsilon_greedy = EpsilonGreedy(deepcopy(base_algorithm), nchoices = nchoices,
                               beta_prior = beta_prior, random_state = 4444)
logistic_ucb = LogisticUCB(nchoices = nchoices, percentile = 70,
                           beta_prior = beta_prior_ts, random_state = 5555)
adaptive_greedy_thr = AdaptiveGreedy(deepcopy(base_algorithm), nchoices=nchoices,
                                     decay_type='threshold',
                                     beta_prior = beta_prior, random_state = 6666)
adaptive_greedy_perc = AdaptiveGreedy(deepcopy(base_algorithm), nchoices = nchoices,
                                      decay_type='percentile', decay=0.9997,
                                       beta_prior=beta_prior, random_state = 7777)
explore_first = ExploreFirst(deepcopy(base_algorithm), nchoices = nchoices,
                             explore_rounds=1500, beta_prior=None, random_state = 8888)
active_explorer = ActiveExplorer(deepcopy(base_algorithm), nchoices = nchoices,
                                 beta_prior=beta_prior, random_state = 9999)
adaptive_active_greedy = AdaptiveGreedy(deepcopy(base_algorithm), nchoices = nchoices,
                                        active_choice='weighted', decay_type='percentile', decay=0.9997,
                                        beta_prior=beta_prior, random_state = 1234)
softmax_explorer = SoftmaxExplorer(deepcopy(base_algorithm), nchoices = nchoices,
                                   beta_prior=beta_prior, random_state = 5678)

models = [bootstrapped_ucb, bootstrapped_ts, one_vs_rest, epsilon_greedy, logistic_ucb,
          adaptive_greedy_thr, adaptive_greedy_perc, explore_first, active_explorer,
          adaptive_active_greedy, softmax_explorer]"

I got an error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-f97973a242eb> in <module>
      1 from sklearn.linear_model import LogisticRegression
----> 2 from contextualbandits.online import BootstrappedUCB, BootstrappedTS, LogisticUCB, \
      3             SeparateClassifiers, EpsilonGreedy, AdaptiveGreedy, ExploreFirst, \
      4             ActiveExplorer, SoftmaxExplorer
      5 from copy import deepcopy

C:\Python38\lib\site-packages\contextualbandits\__init__.py in <module>
----> 1 from . import online
      2 from . import offpolicy
      3 from . import evaluation
      4 from . import linreg

C:\Python38\lib\site-packages\contextualbandits\online.py in <module>
      2 
      3 import numpy as np, warnings, ctypes
----> 4 from .utils import _check_constructor_input, _check_beta_prior, \
      5             _check_smoothing, _check_fit_input, _check_X_input, _check_1d_inp, \
      6             _ZeroPredictor, _OnePredictor, _OneVsRest,\

C:\Python38\lib\site-packages\contextualbandits\utils.py in <module>
      8 from sklearn.linear_model import LogisticRegression
      9 from sklearn.tree import DecisionTreeClassifier
---> 10 from .linreg import LinearRegression, _wrapper_double
     11 from ._cy_utils import _matrix_inv_symm, _create_node_counters
     12 

C:\Python38\lib\site-packages\contextualbandits\linreg\__init__.py in <module>
      4 import warnings
      5 from sklearn.base import BaseEstimator
----> 6 from . import _wrapper_double, _wrapper_float
      7 
      8 __all__ = ["LinearRegression", "ElasticNet"]

contextualbandits\linreg\linreg_double.pyx in init contextualbandits.linreg._wrapper_double()

ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

Does anyone have any idea why it happened?

david-cortes commented 3 years ago

There's a note about it in the readme installation instructions. In short: either update numpy, or install without pep517.

YanjieHe commented 3 years ago

Hi @david-cortes ,

Thank you for your speedy reply! I uninstalled the contextualbandits package and reinstalled it with the command given in the tutorial. But I still got the same error.

python -m pip uninstall contextualbandits
python -m pip install --no-use-pep517 contextualbandits

Any suggestions?

Note: to make sure that I successfully reinstalled the package. I uninstalled the package, and then I restarted the jupyter server, confirming the package has been successfully removed. After that, I installed the package again according to the command in the readme file.

david-cortes commented 3 years ago

That's weird. Perhaps updating numpy to >= 1.20.0 would do (might also need updating cython too). Alternatively, you could also install it without pip with python setup.py install, or if you know how to create installable packages, perhaps modifying the requirements.txt by changing numpy to numpy<=1.19.0.

YanjieHe commented 3 years ago

Hi @david-cortes ,

I upgraded my numpy version to 1.20.1 and it works. Thank you very much for your help!