Scikit Learn Adapter#

Adapter objects for using scikit-learn learners in CapyMOA.

class capymoa.base.SKClassifier[source]#

Bases: Classifier

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

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

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

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

>>> from sklearn.linear_model import SGDClassifier
>>> from capymoa.base import SKClassifier
>>> from capymoa.datasets import ElectricityTiny
>>> stream = ElectricityTiny()
>>> sklearner = SGDClassifier(random_state=1)
>>> learner = SKClassifier(sklearner, stream.schema)
>>> for _ in range(10):
...     instance = stream.next_instance()
...     prediction = learner.predict(instance)
...     print(f"True: {instance.y_index}, Predicted: {prediction}")
...     learner.train(instance)
True: 1, Predicted: None
True: 1, Predicted: 1
True: 1, Predicted: 1
True: 1, Predicted: 1
True: 0, Predicted: 1
True: 0, Predicted: 1
True: 0, Predicted: 0
True: 0, Predicted: 0
True: 0, Predicted: 0
True: 0, Predicted: 0

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

See also capymoa.base.SKRegressor for scikit-learn regressors.

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

Construct a scikit-learn classifier 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: ClassifierMixin#

The underlying scikit-learn object.

train(instance: LabeledInstance)[source]#
predict(instance: Instance)[source]#
predict_proba(instance: Instance)[source]#
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]#