PassiveAggressiveClassifier#

class capymoa.classifier.PassiveAggressiveClassifier[source]#

Bases: SKClassifier

Streaming Passive Aggressive Classifier.

Streaming Passive Aggressive Classifier [1] is a classifier. This wraps 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.

>>> 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
__init__(
schema: Schema,
max_step_size: float = 1.0,
fit_intercept: bool = True,
loss: Literal['hinge', 'squared_hinge'] = 'hinge',
n_jobs: int | None = None,
class_weight: dict[int, float] | None | Literal['balanced'] = None,
average: bool = False,
random_seed=1,
)[source]#

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 – If loss is not one of the supported losses.

classmethod from_params(
schema: Any = None,
params: dict[str, Any] | None = None,
random_seed: int = 1,
) → Any[source]#

Construct an instance from parameters produced by get_params.

get_params() → dict[str, Any][source]#

Return the hyper-parameters captured from the constructor.

predict(
instance: Instance,
) → int | None[source]#

Predict the label of an instance.

The base implementation calls 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,
) → ndarray[tuple[Any, ...], dtype[float64]] | None[source]#

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)[source]#

Train the classifier with a labeled instance.

Parameters:

instance – The labeled instance to train the classifier with.

random_seed: int#

The random seed for reproducibility.

When implementing a classifier ensure random number generators are seeded.

schema: Schema#

The schema representing the instances.

sklearner: SGDClassifier#

The underlying scikit-learn object. See: sklearn.linear_model.SGDClassifier