Skip to content

BanditRegressor

Bandit-based model selection.

Each model is associated with an arm. At each learn_one call, the policy decides which arm/model to pull. The reward is the performance of the model on the provided sample. The predict_one method uses the current best model.

Parameters

  • models

    The models to select from.

  • metric (river.metrics.base.RegressionMetric)

    The metric that is used to measure the performance of each model.

  • policy (river.bandit.base.Policy)

    The bandit policy to use.

Attributes

  • best_model

    The current best model.

  • models

Examples

>>> from river import bandit
>>> from river import datasets
>>> from river import evaluate
>>> from river import linear_model
>>> from river import metrics
>>> from river import model_selection
>>> from river import optim
>>> from river import preprocessing

>>> models = [
...     linear_model.LinearRegression(optimizer=optim.SGD(lr=lr))
...     for lr in [0.0001, 0.001, 1e-05, 0.01]
... ]

>>> dataset = datasets.TrumpApproval()
>>> model = (
...     preprocessing.StandardScaler() |
...     model_selection.BanditRegressor(
...         models,
...         metric=metrics.MAE(),
...         policy=bandit.EpsilonGreedy(
...             epsilon=0.1,
...             decay=0.001,
...             burn_in=100,
...             seed=42
...         )
...     )
... )
>>> metric = metrics.MAE()

>>> evaluate.progressive_val_score(dataset, model, metric)
MAE: 2.544434

Here's another example using the UCB policy. The latter is more sensitive to the target scale, and usually works better when the target is rescaled.

>>> models = [
...     linear_model.LinearRegression(optimizer=optim.SGD(lr=lr))
...     for lr in [0.0001, 0.001, 1e-05, 0.01]
... ]

>>> model = (
...     preprocessing.StandardScaler() |
...     preprocessing.TargetStandardScaler(
...         model_selection.BanditRegressor(
...             models,
...             metric=metrics.MAE(),
...             policy=bandit.UCB(
...                 delta=1,
...                 burn_in=100
...             )
...         )
...     )
... )
>>> metric = metrics.MAE()

>>> evaluate.progressive_val_score(dataset, model, metric)
MAE: 0.857745

Methods

append

S.append(value) -- append value to the end of the sequence

Parameters

  • item
clear

S.clear() -> None -- remove all items from S

copy
count

S.count(value) -> integer -- return number of occurrences of value

Parameters

  • item
extend

S.extend(iterable) -- extend sequence by appending elements from the iterable

Parameters

  • other
index

S.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.

Parameters

  • item
  • args
insert

S.insert(index, value) -- insert value before index

Parameters

  • i
  • item
learn_one

Fits to a set of features x and a real-valued target y.

Parameters

  • x (dict)
  • y (numbers.Number)

Returns

Regressor: self

pop

S.pop([index]) -> item -- remove and return item at index (default last). Raise IndexError if list is empty or index is out of range.

Parameters

  • i – defaults to -1
predict_one

Predict the output of features x.

Parameters

  • x

Returns

The prediction.

remove

S.remove(value) -- remove first occurrence of value. Raise ValueError if the value is not present.

Parameters

  • item
reverse

S.reverse() -- reverse IN PLACE

sort