david-cortes / contextualbandits

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

Can't Find Attribute to Access Base Algorithm Feature Weights (coeff_) #73

Closed nwilder closed 1 month ago

nwilder commented 1 month ago

I have a BootstrappedUCB object instantiated with a SGDClassifier (log_loss) as the base_algorithm, testing with four recommendation arms, and have called partial_fit() for the first time on the object. I believe that one binary classifier is instantiated for each arm behind the scenes. After the first call to fit() or partialfit(), I would like to access the weights (coeff) for the base_algorithms but can't seem to find a way to do so. Is this currently supported?

david-cortes commented 1 month ago

Yes, should be possible - for BootstrappedUCB, the objects are kept under these attributes:

policy._oracles.algos[<arm>].bs_algos[<sample>]

But note that depending on beta_prior, they might correspond to other internal object classes instead of your classifier.

For non-bootstrapped methods, they should be under:

policy._oracles.algos[<arm>]

Example:

import numpy as np
from sklearn.linear_model import LogisticRegression
from contextualbandits.online import BootstrappedUCB
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(100,10))
r = rng.integers(2, size=100)
a = rng.integers(3, size=100)

policy = BootstrappedUCB(LogisticRegression(), 3)
policy.fit(X, a, r)
policy._oracles.algos[0].bs_algos[0].coef_
nwilder commented 1 month ago

Thank you!