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

object has no attribute '_oracles' #17

Closed lesterlitch closed 5 years ago

lesterlitch commented 5 years ago

Hi there,

Wherever I try to predict using bootstrapped_ucb, active_explorer, adaptive_active_greedy etc. with a SGD classifier as the base, I'm seeing the error below:


AttributeError Traceback (most recent call last)

in ----> 1 adaptive_active_greedy.predict(batch_S) ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/contextualbandits/online.py in predict(self, X, exploit) 910 """ 911 # TODO: add option to output scores --> 912 return self._name_arms(self._predict(X, exploit)) 913 914 def _predict(self, X, exploit = False): ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/contextualbandits/online.py in _predict(self, X, exploit) 931 # case 1: number of predictions to make would still fit within current window 932 if remainder_window > X.shape[0]: --> 933 pred, pred_max = self._calc_preds(X) 934 self.window_cnt += X.shape[0] 935 self.window = np.r_[self.window, pred_max] ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/contextualbandits/online.py in _calc_preds(self, X) 975 976 def _calc_preds(self, X): --> 977 pred_proba = self._oracles.decision_function(X) 978 pred_max = pred_proba.max(axis = 1) 979 pred = np.argmax(pred_proba, axis = 1) AttributeError: 'AdaptiveGreedy' object has no attribute '_oracles'
lesterlitch commented 5 years ago

The problems was I forgot to fit the models before they predicted, which I guess is a difference with these types of models where you needs some initial actions before you can start training. Possibly this could be tidied with a "not fitted" error - would you consider a pull request to implement this? Or even a random start method to prevent having to run a first round manually?

david-cortes commented 5 years ago

I think that's something I can add. Issue back then was that scikit-learn's SGD classifier would not be able to do things like extract coefficients before being fitted, but by now that has already been solved.