K-nearest neighbors (KNN) scores a record by its average distance to its k nearest neighboring records. A large score indicates that nearby records are relatively far away, making the record a candidate outlier—not necessarily a meaningful anomaly. Unlike a test built around one average and variance, neighbor distances can flag unusual values between clusters as well as values at the extremes. section 3.2.1 section 2.1.6
A related detector, kth-nearest neighbors, uses only the distance to the kth neighbor. Averaging distances across the k neighbors can be more robust. Both approaches extend from a single feature to multidimensional records; features must first be scaled so their distances are comparable. section 3.2.1
A practical KNN outlier check follows these steps:
- Prepare and scale the features used to compare records. section 3.2.1 section 4.11
- Use a neighbor-search structure such as scikit-learn’s BallTree to retrieve distances, then average each record’s returned distances to obtain a score. section 4.11 section 3.2.1
- Inspect the score distribution, choose records to investigate, and examine their feature values and plots. Vary k rather than assuming one setting is reliable. section 4.11
What a high score may reveal
With two synthetic clusters, a KNN calculation using k = 3 assigns scores that can be displayed as point sizes, then marks the five or fifteen highest-scoring records. On prepared baseball data, a k = 4 detector chiefly flags extreme feature values; its score distribution alone does not establish that those are the most interesting rows. One- and two-feature plots help interpret the flags but may miss patterns in higher dimensions. section 3.2.1 section 4.11
Listing 4.6 Running KNN on the baseball data
from sklearn.neighbors import BallTree tree = BallTree(df) dist, ind = tree.query(df, k=4) dist = [x.mean() for x in dist] df['KNN Score'] = dist df['KNN Outliers'] = df['KNN Score'] > 2.3 sns.histplot(df['KNN Score']) plt.show()
The baseball implementation queries a BallTree with k = 4, averages the returned distances, and applies an example threshold to create labels for plotting. section 4.11


