Implementation:Scikit learn Scikit learn PassiveAggressiveClassifier
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Online Learning |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for online passive-aggressive classification with support for partial_fit incremental learning provided by scikit-learn.
Description
PassiveAggressiveClassifier implements the Passive Aggressive family of algorithms for online learning classification. It extends BaseSGDClassifier and supports two variants: PA-I (maximum step size bounded by C) and PA-II (regularized step size controlled by C). The classifier is "passive" when the prediction is correct (no update) and "aggressive" when there is a misclassification (updates the model to correct the mistake). Note: this class is deprecated since version 1.8 and will be removed in 1.10; the recommended replacement is SGDClassifier(loss='hinge', penalty=None, learning_rate='pa1', eta0=1.0).
Usage
Use PassiveAggressiveClassifier for online or streaming classification tasks where data arrives sequentially and you need to update the model incrementally via partial_fit. It is suited for large-scale learning where you cannot fit the entire dataset in memory, and when you want a simple margin-based classifier that adapts aggressively to misclassified examples.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/linear_model/_passive_aggressive.py
Signature
class PassiveAggressiveClassifier(BaseSGDClassifier):
"""Passive Aggressive Classifier (deprecated in 1.8)."""
def __init__(
self,
*,
C=1.0,
fit_intercept=True,
max_iter=1000,
tol=1e-3,
early_stopping=False,
validation_fraction=0.1,
n_iter_no_change=5,
shuffle=True,
verbose=0,
loss="hinge",
n_jobs=None,
random_state=None,
warm_start=False,
class_weight=None,
average=False,
):
Import
from sklearn.linear_model import PassiveAggressiveClassifier
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| C | float | No | Aggressiveness parameter; max step size for PA-I, regularization for PA-II (default=1.0) |
| fit_intercept | bool | No | Whether to estimate the intercept (default=True) |
| max_iter | int | No | Maximum number of passes over the training data (default=1000) |
| tol | float or None | No | Stopping criterion tolerance (default=1e-3) |
| early_stopping | bool | No | Whether to use early stopping with validation score (default=False) |
| validation_fraction | float | No | Fraction of training data for early stopping validation (default=0.1) |
| n_iter_no_change | int | No | Number of epochs with no improvement before stopping (default=5) |
| shuffle | bool | No | Whether to shuffle training data after each epoch (default=True) |
| loss | str | No | Loss function: 'hinge' or 'squared_hinge' (default='hinge') |
| class_weight | dict or 'balanced' | No | Weights associated with classes (default=None) |
| average | bool or int | No | Whether to compute averaged SGD weights (default=False) |
Outputs
| Name | Type | Description |
|---|---|---|
| coef_ | ndarray of shape (1, n_features) or (n_classes, n_features) | Weights assigned to the features |
| intercept_ | ndarray of shape (1,) or (n_classes,) | Constants in the decision function |
| classes_ | ndarray of shape (n_classes,) | Unique classes labels |
| n_iter_ | int | Number of iterations (epochs) performed |
Usage Examples
Basic Usage
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=200, n_features=10, random_state=42)
model = PassiveAggressiveClassifier(C=1.0, max_iter=1000)
model.fit(X, y)
print("Score:", model.score(X, y))
print("Classes:", model.classes_)