K-Nearest Neighbours (KNN) is a non-parametric, instance-based classifier: it stores the entire training set and, at inference time, classifies each test transaction by looking at the k closest training examples in the 29-dimensional PCA feature space. In the FFD pipeline, the GAN-augmented training set provides enough fraud examples near the decision boundary that the neighbourhood vote is informative even at the defaultDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Antisource/FFD/llms.txt
Use this file to discover all available pages before exploring further.
k=5.
How KNN Works
KNN makes no assumptions about the underlying distribution of features. Instead, it relies on the smoothness assumption: nearby points in feature space are likely to share the same class label. Classification steps:- Compute distances from the query point to every training sample. The default metric is Euclidean distance in the PCA feature space (V1–V28 + scaled Amount).
- Select the k nearest neighbours — the k training samples with the smallest distances.
- Majority vote — the predicted class is the label held by the most neighbours. With
weights='distance', closer neighbours cast heavier votes.
- Lazy learning — no explicit training phase; all computation happens at prediction time, so inference is slower than tree-based models on large test sets.
- Local decision boundary — can capture complex, non-convex fraud clusters that a single global hyperplane (SVM) might miss.
- Sensitive to scaling — PCA-transformed features are already centred and comparably scaled, which is a prerequisite for meaningful Euclidean distances.
Implementation
The notebook usesn_neighbors=5, the library default:
Key Hyperparameters
| Parameter | Notebook value | Guidance |
|---|---|---|
n_neighbors (k) | 5 | Smaller k → more flexible boundary, higher variance. Larger k → smoother boundary, higher bias. Use cross-validation to tune. |
metric | 'minkowski' (p=2, i.e., Euclidean) | 'euclidean' and 'manhattan' are the most common alternatives. Euclidean works well for PCA-transformed data. |
weights | 'uniform' | Set to 'distance' so that closer neighbours contribute more; often improves recall on fraud. |
algorithm | 'auto' | 'ball_tree' or 'kd_tree' can be faster for moderate-dimensional spaces; 'brute' is exact but slow for large n. |
p | 2 | Exponent in the Minkowski metric: p=2 gives Euclidean, p=1 gives Manhattan. |
Evaluation
k or switching to weights='distance'.
See Also
Training Pipeline
End-to-end pipeline: data loading, GAN augmentation, train/test split, and unified evaluation loop.
Metrics Reference
Definitions of accuracy, precision, recall, and F1 as used across all six FFD classifiers.