liveBook · Manning

Radius detector

As explained in Outlier Detection in Python

A radius detector identifies or scores observations according to the number or arrangement of neighboring observations within a specified distance radius.

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

How Outlier Detection in Python covers it

How deep it goes
Explains neighbor counting, score conversion, thresholding, and parameter sensitivity, but does not derive the distance metric.
Examples
Uses a two-dimensional synthetic dataset, BallTree counts within radius 2.0, score histograms, and a top-15 scatterplot.
What it assumes
Assumes familiarity with distances, neighborhoods, and outlier scoring; introduces the radius choice as an experimental tuning problem.
What you can do after
Apply it via BallTree to rank sparse observations and visualize a selected set of flagged points.
How it connects
Connects density-based outlier detection with pairwise distances, KNN, and local outlier factor.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.