# `SGDClassifier`

### *class* capymoa.classifier.SGDClassifier[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/classifier/_sgd_classifier.py#L11)

Bases: [`SKClassifier`](capymoa.base.SKClassifier.md#capymoa.base.SKClassifier)

Streaming stochastic gradient descent classifier.

This wraps [`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) for
ease of use in the streaming context. Some options are missing because
they are not relevant in the streaming context. Furthermore, the learning rate
is constant.

```pycon
>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.classifier import PassiveAggressiveClassifier
>>> from capymoa.evaluation import prequential_evaluation
>>> stream = ElectricityTiny()
>>> schema = stream.get_schema()
>>> learner = SGDClassifier(schema)
>>> results = prequential_evaluation(stream, learner, max_instances=1000)
>>> results["cumulative"].accuracy()
84.2
```

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema), loss: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['hinge', 'log_loss', 'modified_huber', 'squared_hinge', 'perceptron', 'squared_error', 'huber', 'epsilon_insensitive', 'squared_epsilon_insensitive'] = 'hinge', penalty: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['l2', 'l1', 'easticnet'] = 'l2', alpha: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.0001, l1_ratio: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.15, fit_intercept: [bool](https://docs.python.org/3/builtins/functions.html#bool) = True, epsilon: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.1, n_jobs: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, learning_rate: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['constant', 'optimal', 'invscaling'] = 'optimal', eta0: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.01, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/classifier/_sgd_classifier.py#L33)

Construct stochastic gradient descent classifier.

* **Parameters:**
  * **schema** – Describes the datastream’s structure.
  * **loss** – The loss function to be used.
  * **penalty** – The penalty (aka regularization term) to be used.
  * **alpha** – Constant that multiplies the regularization term.
  * **l1_ratio** – The Elastic Net mixing parameter, with `0 <= l1_ratio <= 1`.
    `l1_ratio=0` corresponds to L2 penalty, `l1_ratio=1` to L1.
    Only used if `penalty` is ‘elasticnet’.
    Values must be in the range `[0.0, 1.0]`.
  * **fit_intercept** – Whether the intercept (bias) should be estimated
    or not. If False, the data is assumed to be already centered.
  * **epsilon** – Epsilon in the epsilon-insensitive loss functions; only
    if `loss` is ‘huber’, ‘epsilon_insensitive’, or
    ‘squared_epsilon_insensitive’. For ‘huber’, determines the threshold
    at which it becomes less important to get the prediction exactly right.
    For epsilon-insensitive, any differences between the current prediction
    and the correct label are ignored if they are less than this threshold.
  * **n_jobs** – The number of CPUs to use to do the OVA (One Versus All, for
    multi-class problems) computation. Defaults to 1.
  * **learning_rate** – The size of the gradient step.
  * **eta0** – The initial learning rate for the ‘constant’, ‘invscaling’ or
    ‘adaptive’ schedules. The default value is 0.0 as `eta0` is not used by
    the default schedule ‘optimal’.
  * **class_weight** – 

    Weights associated with classes. If not given, all classes
    are supposed to have weight one.

    The “balanced” mode uses the values of y to automatically adjust
    weights inversely proportional to class frequencies in the input data
    as `n_samples / (n_classes * np.bincount(y))`.
  * **random_seed** – Seed for reproducibility.

#### *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.classifier.SGDClassifier.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 *: [SGDClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier)*

The underlying scikit-learn object
