KNNDetector<T>
Models & Types · AiDotNet.AnomalyDetection.DistanceBased
Detects anomalies using K-Nearest Neighbors distance.
Inheritance: ModelBase<T, Matrix<T>, Vector<T>> → AnomalyDetectorBase<T> → KNNDetector<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
KNN anomaly detection identifies outliers based on their distance to their k-nearest neighbors. Points that are far from their nearest neighbors are considered anomalies.
How It Works
The algorithm works by:
- For each point, find the k nearest neighbors
- Calculate the average distance to these neighbors
- Points with large average distances are anomalies
When to use:
- When anomalies are expected to be isolated from normal clusters
- Works well with low-to-medium dimensional data
- No assumption about data distribution
Industry Standard Defaults:
- K (neighbors): 5 (typical range 3-20)
- Contamination: 0.1 (10%)
Constructors
| Constructor | Summary |
|---|---|
KNNDetector(int k, double contamination, int randomSeed) |
Creates a new KNN anomaly detector. |
Properties
| Property | Summary |
|---|---|
int K { get; } |
Gets the number of neighbors used for detection. |
Methods
| Method | Summary |
|---|---|
Vector<T> ScoreAnomalies(Matrix<T> X) |
|
void Fit(Matrix<T> X) |