# `PassiveAggressiveClassifier`

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

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

Streaming Passive Aggressive Classifier.

Streaming Passive Aggressive Classifier <sup>[1](#id2)</sup> is a classifier. This wraps
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) with a passive aggressive
learning rate schedule for ease of use in the streaming context. Some
options are missing because they are not relevant in the streaming context.

```pycon
>>> from capymoa.classifier import PassiveAggressiveClassifier
>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.evaluation import prequential_evaluation
>>>
>>> stream = ElectricityTiny()
>>> classifier = PassiveAggressiveClassifier(stream.get_schema())
>>> results = prequential_evaluation(stream, classifier, max_instances=1000)
>>> print(f"{results['cumulative'].accuracy():.1f}")
84.3
```

* <a id='id2'>**[1]**</a> [Online Passive-Aggressive Algorithms K. Crammer, O. Dekel, J. Keshat, S. Shalev-Shwartz, Y. Singer - JMLR (2006)](http://jmlr.csail.mit.edu/papers/volume7/crammer06a/crammer06a.pdf)

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema), max_step_size: [float](https://docs.python.org/3/builtins/functions.html#float) = 1.0, fit_intercept: [bool](https://docs.python.org/3/builtins/functions.html#bool) = True, loss: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['hinge', 'squared_hinge'] = 'hinge', n_jobs: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, class_weight: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[int](https://docs.python.org/3/builtins/functions.html#int), [float](https://docs.python.org/3/builtins/functions.html#float)] | [None](https://docs.python.org/3/builtins/constants.html#None) | [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['balanced'] = None, average: [bool](https://docs.python.org/3/builtins/functions.html#bool) = False, random_seed=1)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/classifier/_passive_aggressive_classifier.py#L41)

Construct a passive aggressive classifier.

* **Parameters:**
  * **schema** – Stream schema
  * **max_step_size** – Maximum step size (regularization).
  * **fit_intercept** – Whether the intercept should be estimated or not.
    If False, the data is assumed to be already centered.
  * **loss** – The loss function to be used: hinge: equivalent to PA-I in
    the reference paper. squared_hinge: equivalent to PA-II in the reference paper.
  * **n_jobs** – The number of CPUs to use to do the OVA (One Versus All,
    for multi-class problems) computation. None means 1 unless in a
    `joblib.parallel_backend` context. -1 means using all processors.
  * **class_weight** – 

    Preset for the `sklearner.class_weight` fit parameter.

    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))`.
  * **average** – When set to True, computes the averaged SGD weights and
    stores the result in the `sklearner.coef_` attribute. If set to an int greater
    than 1, averaging will begin once the total number of samples
    seen reaches average. So `average=10` will begin averaging after
    seeing 10 samples.
  * **random_seed** – Seed for the random number generator.
* **Raises:**
  [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If `loss` is not one of the supported losses.

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