PassiveAggressiveClassifier<T>
Models & Types · AiDotNet.Classification.Linear
Passive-Aggressive classifier for online learning.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → LinearClassifierBase<T> → PassiveAggressiveClassifier<T>
For Beginners
Unlike regular gradient descent, Passive-Aggressive:
- Only updates when it makes a mistake
- When it updates, it does the minimum needed to fix the mistake
It’s great for:
- Online learning (data arrives one sample at a time)
- Streaming data
- When you want a balance between learning and stability
The regularization parameter C controls the aggressiveness:
- Higher C: More aggressive updates (may overfit to noise)
- Lower C: More passive (may underfit)
How It Works
The Passive-Aggressive algorithm is an online learning algorithm that:
- Is “passive” when the prediction is correct (no update)
- Is “aggressive” when wrong (makes the minimal update to correct the mistake)
Example
// Create passive-aggressive classifier for online learning
var options = new PassiveAggressiveOptions<double>();
// Prepare training data
var features = new Matrix<double>(new double[,] { { 1.0, 1.1 }, { 1.2, 0.9 }, { 0.8, 1.0 }, { 5.0, 5.1 }, { 5.2, 4.9 }, { 4.8, 5.0 } });
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1 });
// Train with minimal weight updates on correct predictions
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new PassiveAggressiveClassifier<double>(options))
.Build(features, labels);
// Predict class for new sample
var newSample = new Matrix<double>(new double[,] { { 1.1, 1.0 } });
var prediction = result.Predict(newSample);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
PassiveAggressiveClassifier(PassiveAggressiveOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) |
Initializes a new instance of the PassiveAggressiveClassifier class. |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |
|
void Train(Matrix<T> x, Vector<T> y) |
Returns the model type identifier for this classifier. |