detect method
Implementation
List<AnomalyResult> detect(List<List<double>> data) {
DataValidator.validateNonEmpty(data);
DataValidator.validateEqualLengthVectors(data);
final int k = config.minPoints;
final distanceType = config.distanceType;
final List<List<double>> distances = _computeDistances(data, distanceType);
final List<List<int>> neighbors = _findKNearestNeighbors(distances, k);
final List<double> lrdValues = _computeLRD(data, distances, neighbors, k);
final List<AnomalyResult> results = [];
for (int i = 0; i < data.length; i++) {
double lofScore = 0.0;
for (int n in neighbors[i]) {
lofScore += lrdValues[n] / lrdValues[i];
}
lofScore /= k;
bool isAnomaly = lofScore > config.threshold;
results.add(AnomalyResult(
value: 0.0, // Placeholder, because multivariate
featureVector: data[i],
index: i,
score: lofScore,
isAnomaly: isAnomaly,
algorithm: 'lof',
confidence: (lofScore / config.threshold).clamp(0.0, 1.0),
explanation: isAnomaly
? 'LOF score $lofScore exceeds threshold ${config.threshold}.'
: 'LOF score $lofScore is within acceptable range.',
));
}
return results;
}