A radius detector identifies sparse observations by counting how many points lie within a chosen distance radius. Few neighbors indicate a low-density region and produce a higher outlier score. section 3.2.2
A basic radius-based detection procedure is:
- Compute pairwise distances or construct a spatial search structure such as a BallTree. section 3.2.2
- Count the observations within the selected radius for each point. section 3.2.2
- Convert counts into scores, for example by assigning larger scores to smaller counts. section 3.2.2
- Choose a cutoff or retain a specified number of highest-scoring observations, then inspect the flagged points. section 3.2.2
The radius is the central practical choice. Unlike KNN, which requires selecting k, Radius requires a distance value that is often unintuitive and usually must be determined experimentally. Different radii can substantially change the resulting scores. section 3.3.7
Synthetic two-dimensional data
For each point, a BallTree counts neighbors within radius 2.0. The counts are plotted, converted directly into outlier scores, and used to display the top 15 outliers on a scatterplot. section 3.2.2
Listing 3.2 Radius outler detection
tree = BallTree(df)
counts = tree.query_radius(df, 2.0, count_only=True)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8, 4))
s = sns.histplot(counts, ax=ax[0]) #1
s.set_title(f"Number of Points within Radius")
min_score = min(counts) #2
max_score = max(counts)
scores = [(max_score - x)/(max_score - min_score) for x in counts]
s = sns.histplot(scores, ax=ax[1])
s.set_title(f"Scores")
df['Score'] = scores #3
threshold = sorted(scores, reverse=True)[15]
df_flagged = df[df['Score'] >= threshold]
s = sns.scatterplot(data=df, x="A", y="B", ax=ax[2])
s = sns.scatterplot(data=df_flagged, x="A", y="B", color='red',
marker='*', s=200, ax=ax[2])
plt.tight_layout()
plt.show()
Inspect the concise Python implementation for neighbor counting, score normalization, thresholding, and visual flagging. section 3.2.2
