apollon.mab¶
mab ¶
Apollon: per-cluster Multi-Armed Bandit with Thompson sampling over a pool of IDS classifiers.
How a request is answered:
- Inputs are grouped with K-Means and every arm (classifier) is trained on each cluster, so each cluster has its own specialised pool.
- Each (cluster, arm) pair keeps a Beta posterior over the arm's probability of classifying a request correctly. The posteriors start from the arms' performance on a held-out validation split.
- For every request, Thompson sampling draws one value per arm from its posterior and the arm with the highest draw answers. Better arms answer more often, but the answering model keeps changing from request to request, so an attacker cannot learn a single, stable decision boundary to craft adversarial examples against.
- When ground truth becomes available (for instance, analyst feedback), :meth:
updateadds the observed successes and failures to the posteriors, so the selection keeps adapting to the traffic.
ApollonMAB ¶
ApollonMAB(arms=None, n_clusters: int = 2, val_size: float = 0.2, prior_strength: float | None = None, random_state: int | None = None)
Bases: ClassifierMixin, BaseEstimator
Per-cluster Thompson-sampling selection over a pool of binary classifiers.
The estimator follows the scikit-learn API (fit, predict, predict_proba,
get_params), so it can be used wherever a classifier is expected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arms
|
Unfitted scikit-learn classifiers. |
None
|
|
n_clusters
|
int
|
Number of K-Means clusters (the paper uses 2). |
2
|
val_size
|
float
|
Fraction of the training data held out to initialise the Beta posteriors. |
0.2
|
prior_strength
|
float | None
|
If set, the validation counts of each (cluster, arm) are rescaled to this many
pseudo-observations, which keeps the selection more mixed. |
None
|
random_state
|
int | None
|
Seed for the split, K-Means, the arms and the Thompson draws. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
kmeans_ |
Fitted K-Means model. |
|
models_ |
|
|
alpha_, beta_ |
Beta posterior parameters, shape |
|
shared_ |
|
Examples:
>>> from sklearn.datasets import make_classification
>>> from apollon import ApollonMAB
>>> X, y = make_classification(n_samples=500, random_state=0)
>>> model = ApollonMAB(random_state=0).fit(X, y)
>>> labels, arms = model.predict(X[:5], return_arms=True)
Source code in src/apollon/mab.py
fit ¶
Cluster the data, train every arm per cluster and initialise the posteriors.
Source code in src/apollon/mab.py
assign_clusters ¶
select_arms ¶
Cluster and Thompson-selected arm for every row of X.
predict ¶
Predicted label of every row (one Thompson draw per row).
With return_arms=True also returns the index of the arm that answered.
Source code in src/apollon/mab.py
predict_proba ¶
update ¶
Update the posteriors with ground truth for requests already answered.
Pass the clusters/arms returned when the requests were answered (and the
predictions) to credit exactly the arms that answered. Without them, the requests
are answered again now.
Source code in src/apollon/mab.py
selection_frequencies ¶
Share of requests answered by each arm.
Source code in src/apollon/mab.py
posterior_means ¶
save_model ¶
Save a fitted model with joblib.
load_model ¶
Load a model saved with :func:save_model.
Only load files you trust: joblib files can execute code when loaded.