Train a linear classifier on large datasets
Large-scale supervised classification
Scikit-Learn · Class
PassiveAggressiveClassifier is a linear online classifier using passive-aggressive updates. It supports hinge loss for PA-I and squared hinge loss for PA-II, but the class is deprecated in scikit-learn 1.8 and planned for removal in 1.10.
The estimator learns a linear decision boundary with an online passive-aggressive update rule: it leaves the model unchanged for correctly classified samples with sufficient margin and updates it when the margin is violated. The hinge loss gives PA-I, while squared hinge gives PA-II; fitting can proceed in epochs, and partial_fit supports incremental batches.
class sklearn.linear_model.PassiveAggressiveClassifier(*, C=1.0, fit_intercept=True, max_iter=1000, tol=0.001, 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)| Name | Type | Description |
|---|---|---|
C | float, default=1.0 | Aggressiveness parameter for the passive-aggressive algorithm. |
fit_intercept | bool, default=True | Whether to fit the intercept. |
max_iter | int, default=1000 | Maximum number of passes over the training data. |
tol | float, default=0.001 | Stopping criterion tolerance. |
early_stopping | bool, default=False | Whether to stop early using a validation fraction. |
validation_fraction | float, default=0.1 | Proportion of training data reserved for validation when early stopping is enabled. |
n_iter_no_change | int, default=5 | Number of iterations without improvement before stopping. |
shuffle | bool, default=True | Whether to shuffle training data after each epoch. |
verbose | int, default=0 | Verbosity level. |
loss | str, default='hinge' | Loss function; 'hinge' corresponds to PA-I and 'squared_hinge' to PA-II. |
n_jobs | int, default=None | Number of CPUs used for one-vs-rest computation. |
random_state | int, RandomState instance, or None, default=None | Controls pseudo-randomness for shuffling and related operations. |
An estimator object; fit and partial_fit return self.
from sklearn.datasets import make_classification
from sklearn.linear_model import PassiveAggressiveClassifier
X, y = make_classification(n_samples=100, n_features=4, random_state=0)
clf = PassiveAggressiveClassifier(max_iter=1000, random_state=0)
clf.fit(X, y)
print(clf.predict(X[:5]))Creates a small binary dataset, trains the classifier, and predicts labels for five samples.
Large-scale supervised classification
Streaming or batch-at-a-time learning
Online text classification
It is an online linear classifier based on the passive-aggressive algorithm. Its C parameter controls aggressiveness, and it supports hinge or squared-hinge loss.
Follow-up: How would you replace it? Use SGDClassifier with loss='hinge', penalty=None, learning_rate='pa1' or 'pa2', and eta0=1.0 for the corresponding C setting.
fit trains over the data for up to max_iter epochs, while partial_fit updates the model from an individual batch and does not use max_iter.
Follow-up: Why use partial_fit? It enables incremental learning when samples arrive in batches or the complete dataset cannot be held in memory.
The hinge loss implements PA-I, and squared_hinge implements PA-II. The classifier is intended for large-scale learning and does not require a conventional learning-rate parameter.
Follow-up: What controls the update behavior? C is the aggressiveness parameter, while loss determines the PA-I versus PA-II update formulation.
SGDClassifierPassiveAggressiveRegressorPerceptronsklearn.linear_model