Overview
Approximate Large Margin Algorithm (ALMA) is an online linear classifier that maintains a margin-based weight vector for binary classification.
Description
ALMA is an online learning algorithm that updates weights based on the margin between predictions and a threshold. The algorithm uses a perceptron-like update rule but with explicit margin control through parameters p (norm order), alpha (margin fraction), B (margin scaling), and C (learning rate scaling). The weights are normalized after each update to maintain a bounded norm, and updates only occur when the prediction margin falls below a threshold.
Usage
Use ALMA when you need an online binary classifier with explicit margin control and want better generalization than standard perceptron. It's particularly effective for linearly separable problems with clear margin requirements and works well with feature scaling preprocessing.
Code Reference
Source Location
Signature
class ALMAClassifier(base.Classifier):
def __init__(self, p=2, alpha=0.9, B=1 / 0.9, C=2**0.5):
self.p = p
self.alpha = alpha
self.B = B
self.C = C
self.w = collections.defaultdict(float)
self.k = 1
Import
from river import linear_model
I/O Contract
Parameters
| Parameter |
Type |
Default |
Description
|
| p |
int |
2 |
Norm order for weight normalization
|
| alpha |
float |
0.9 |
Margin fraction parameter
|
| B |
float |
1/0.9 |
Margin scaling parameter
|
| C |
float |
sqrt(2) |
Learning rate scaling parameter
|
Attributes
| Attribute |
Type |
Description
|
| w |
collections.defaultdict |
Current weight vector
|
| k |
int |
Number of training instances seen
|
Input
| Method |
Input Type |
Description
|
| learn_one |
x: dict, y: bool |
Feature dict and binary target
|
| predict_proba_one |
x: dict |
Feature dict for prediction
|
Output
| Method |
Output Type |
Description
|
| predict_proba_one |
dict |
Probabilities for False and True classes
|
| predict_one |
bool |
Predicted class (inherited)
|
Usage Examples
from river import datasets
from river import evaluate
from river import linear_model
from river import metrics
from river import preprocessing
dataset = datasets.Phishing()
model = (
preprocessing.StandardScaler() |
linear_model.ALMAClassifier()
)
metric = metrics.Accuracy()
evaluate.progressive_val_score(dataset, model, metric)
# Accuracy: 82.56%
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.