# `SKClassifier`

### *class* capymoa.base.SKClassifier[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L138)

Bases: [`Classifier`](capymoa.base.Classifier.md#capymoa.base.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`](capymoa.classifier.md#module-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:

```pycon
>>> 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.get_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`](capymoa.base.SKRegressor.md#capymoa.base.SKRegressor) for scikit-learn regressors.

#### \_\_init_\_(sklearner: [ClassifierMixin](https://scikit-learn.org/stable/modules/generated/sklearn.base.ClassifierMixin.html#sklearn.base.ClassifierMixin), schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L184)

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**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If the scikit-learn algorithm does not implement
  `partial_fit` or `predict`.

#### *classmethod* from_params(schema: [Any](https://docs.python.org/3/library/typing.html#typing.Any) = None, params: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1) → [Any](https://docs.python.org/3/library/typing.html#typing.Any)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L170)

Construct an instance from parameters produced by `get_params`.

#### get_params() → [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)][[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L163)

Return the hyper-parameters captured from the constructor.

#### predict(instance: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L218)

Predict the label of an instance.

The base implementation calls [`predict_proba()`](#capymoa.base.SKClassifier.predict_proba) and returns the
label with the highest probability.

* **Parameters:**
  **instance** – The instance to predict the label for.
* **Returns:**
  The predicted label or `None` if the classifier is unable
  to make a prediction.

#### predict_proba(instance: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[Any](https://docs.python.org/3/library/typing.html#typing.Any), ...], [dtype](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html#numpy.dtype)[float64]] | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L224)

Return probability estimates for each label.

* **Parameters:**
  **instance** – The instance to estimate the probabilities for.
* **Returns:**
  An array of probabilities for each label or `None` if the
  classifier is unable to make a prediction.

#### train(instance: [LabeledInstance](capymoa.core.LabeledInstance.md#capymoa.core.LabeledInstance))[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L210)

Train the classifier with a labeled instance.

* **Parameters:**
  **instance** – The labeled instance to train the classifier with.

#### random_seed *: [int](https://docs.python.org/3/builtins/functions.html#int)*

The random seed for reproducibility.

When implementing a classifier ensure random number generators are seeded.

#### schema *: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema)*

The schema representing the instances.

#### sklearner *: [ClassifierMixin](https://scikit-learn.org/stable/modules/generated/sklearn.base.ClassifierMixin.html#sklearn.base.ClassifierMixin)*

The underlying scikit-learn object.
