SKRegressor#

class capymoa.base.SKRegressor[source]#

Bases: Regressor

A wrapper class for using scikit-learn regressors in CapyMOA.

Some of scikit-learn’s regressors that are compatible with online learning have been wrapped and tested already in CapyMOA (See capymoa.regressor).

However, if you want to use a scikit-learn regressor that has not been wrapped yet, you can use this class to wrap it yourself. This requires that the scikit-learn regressor implements the partial_fit and predict methods.

For example, the following code demonstrates how to use a scikit-learn regressor in CapyMOA:

>>> from sklearn.linear_model import SGDRegressor
>>> from capymoa.datasets import Fried
>>> stream = Fried()
>>> sklearner = SGDRegressor(random_state=1)
>>> learner = SKRegressor(sklearner, stream.schema)
>>> for _ in range(10):
...     instance = stream.next_instance()
...     prediction = learner.predict(instance)
...     if prediction is not None:
...         print(f"y_value: {instance.y_value}, y_prediction: {prediction:.2f}")
...     else:
...         print(f"y_value: {instance.y_value}, y_prediction: None")
...     learner.train(instance)
y_value: 17.949, y_prediction: None
y_value: 13.815, y_prediction: 0.60
y_value: 20.766, y_prediction: 1.30
y_value: 18.301, y_prediction: 1.86
y_value: 22.989, y_prediction: 2.28
y_value: 25.986, y_prediction: 2.65
y_value: 17.15, y_prediction: 3.51
y_value: 14.006, y_prediction: 3.25
y_value: 18.566, y_prediction: 3.80
y_value: 12.107, y_prediction: 3.87

A word of caution: even compatible scikit-learn regressors are not necessarily designed for online learning and might require some tweaking to work well in an online setting.

See also capymoa.base.SKClassifier for scikit-learn classifiers.

__init__(
sklearner: RegressorMixin,
schema: Schema = None,
random_seed: int = 1,
)[source]#

Construct a scikit-learn regressor wrapper.

Parameters:
  • sklearner – A scikit-learn classifier object to wrap that must implements partial_fit and predict.

  • schema – Describes the structure of the datastream.

  • random_seed – Random seed for reproducibility.

Raises:

ValueError – If the scikit-learn algorithm does not implement partial_fit or predict.

sklearner: RegressorMixin#

The underlying scikit-learn object.

train(instance: RegressionInstance)[source]#
predict(instance: Instance) float[source]#