milia.anomaly.models.knn#
k-nearest-neighbour distance to the training data.
Classes
|
Distance detector (Ramaswamy et al., 2000): far from its neighbours means anomalous. |
|
Validated hyperparameters of the k-nearest-neighbour detector. |
- class milia.anomaly.models.knn.KNN(n_neighbors=5, method='largest', metric='minkowski', contamination=0.1)#
Bases:
BaseDetectorDistance detector (Ramaswamy et al., 2000): far from its neighbours means anomalous.
Scoring a training row counts the row itself as its nearest neighbour, as any scorer applied to data it was fitted on does; calibrate on held-out normal data (
calibrate()) for a threshold with a guarantee.- Parameters:
Examples
>>> import numpy as np >>> rng = np.random.default_rng(0) >>> train, cal = rng.normal(size=(200, 2)), rng.normal(size=(100, 2)) >>> model = KNN().fit(train).calibrate(cal, alpha=0.1) >>> model.flag(np.array([[0.0, 0.0], [8.0, 8.0]])).tolist() [False, True]
- class milia.anomaly.models.knn.KNNConfig(*, name='knn', kind='detector', space=SearchSpace(n_neighbors=(2, 50), method=['largest', 'mean']), n_neighbors=5, method='largest', metric='minkowski', contamination=0.1)#
Bases:
BaseModelConfigValidated hyperparameters of the k-nearest-neighbour detector.
Fields mirror
KNN’s constructor;spaceis the default Optuna search space. Build the detector withmake("detector", "knn", ...)orKNN(**config.params()).Examples
>>> KNNConfig().name 'knn'