Find peak regions in 2D data
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Finding peak regions in 2D data is usually not about identifying one highest cell. It is about locating connected areas that rise above their surroundings in a stable way, even when the data is noisy.
That makes the problem closer to image segmentation than to a simple max lookup. A practical solution usually combines smoothing, thresholding, local-maximum detection, and connected-component labeling.
What Counts as a Peak Region
In a 2D grid, a peak region is often defined as:
- an area whose values exceed a threshold
- centered around a local maximum
- separated from neighboring peaks by valleys or low-value boundaries
The exact definition depends on the domain. In terrain analysis, a peak may mean elevation. In microscopy, it may mean bright intensity. In heatmaps, it may mean anomaly concentration.
A Simple Workflow
A practical workflow looks like this:
- smooth the data to reduce single-cell noise
- find candidate local maxima
- threshold the grid so weak signals are removed
- label connected components around each candidate
This gives regions rather than isolated points.
A Runnable Python Example
The code below uses a small synthetic matrix and a pure NumPy-style approach so the logic is visible. It marks cells above a threshold and then groups connected neighbors.
This approach finds connected high-value regions and reports the strongest cell inside each region.
Why Smoothing Often Matters
Real 2D data is noisy. If you skip smoothing, tiny fluctuations can produce many false peaks. Common smoothing choices include:
- Gaussian blur
- mean filter
- median filter
The right filter depends on whether you want to preserve sharp boundaries or suppress salt-and-pepper noise.
Separating Nearby Peaks
Thresholding alone can merge nearby hills into one large blob. When peak separation matters, use a stronger method such as watershed segmentation or non-maximum suppression after smoothing.
The core idea is to:
- detect local maxima first
- use those maxima as seeds
- grow regions until boundaries meet
That is how you distinguish two close peaks that share a broad elevated plateau.
Choosing Parameters
The most important parameters are:
- smoothing strength
- threshold level
- neighborhood definition, such as 4-connected or 8-connected
- minimum region size
There is no universal best setting. The correct values depend on signal scale and noise level in the source data.
Common Pitfalls
- Treating the highest single value as the only peak when the goal is to find peak regions.
- Skipping smoothing and detecting noise spikes instead of real structures.
- Using a fixed threshold without checking how data scale varies between samples.
- Merging nearby peaks into one component because the threshold is too low.
- Ignoring whether diagonal neighbors should count as connected.
Summary
- Peak-region detection in 2D data is usually a segmentation problem, not a plain max search.
- A practical pipeline combines smoothing, thresholding, and connected-component labeling.
- Local maxima identify candidate peaks, while connected regions define their extent.
- Parameter choice depends on noise level, scale, and domain meaning.
- If nearby peaks merge, use seed-based or watershed-style separation instead of thresholding alone.
Related reading
- Find records from one table which don't exist in another
- Find row where values for column is maximal in a pandas DataFrame
- Find rows that have the same value on a column in MySQL
- Find Similar Rows in Database
- Find the column name which has the maximum value for each row
- Find the intersection of two curves given by x, y data with high precision in Python
- Find the max of two or more columns with pandas
- Find the most common element in a list
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.