AI Glossary / Mathematics and optimization / Intermediate
Neighborhood
Definition
A neighborhood is a collection of points or nodes treated as local to a reference point under a specified rule. In a metric space, it can be defined by a distance threshold. In a graph, it often means the nodes directly adjacent to a node. The distance, radius, boundary convention, and treatment of the reference point determine its exact meaning.
What it means
“Nearby” is not an intrinsic property of a data row. It depends on how the row is represented and how distances are measured. Customers can be close by purchasing behavior but far apart geographically. Two graph nodes can be neighbors because they share an edge, without any numerical feature distance being specified.
The supplied glossary uses an epsilon-neighborhood with distance at most epsilon. Other texts use a strict inequality for an open ball. General topology uses a broader definition: a neighborhood of a point contains an open set around it. These conventions are related, but they should not be silently interchanged in a technical specification.
How it works
Choose a reference point x, a space of candidates, and a distance d. A closed radius neighborhood can be written {z:d(x,z)≤r}; an open ball uses d(x,z)<r. A dataset neighborhood usually intersects this region with the available observations. State whether the query itself is excluded.
A k-nearest-neighbor rule instead selects a specified number of nearby observations, with a rule for ties. Its effective radius can vary across the dataset. A fixed-radius neighborhood keeps the distance threshold constant but can contain very different numbers of observations in dense and sparse areas.
For graphs, an open neighborhood convention usually lists immediate neighbors without the center; a closed neighborhood adds the center. A multi-hop neighborhood requires a maximum path length and, for directed graphs, a direction convention. The word “open” in graph terminology should not be confused automatically with an open set in a metric space.
Three practical examples
1. Local prediction: estimate a numerical target from nearby training observations, using a chosen radius or neighbor count and an explicit averaging rule.
2. Density analysis: examine how many observations fall within a local radius when exploring clusters or unusually sparse regions.
3. Network exploration: display a node's immediate connections or a bounded number of graph hops, helping a user inspect local relationships without showing an entire large network.
Think of it this way
Think of drawing a service area around a shop. A fixed travel-distance limit can contain many addresses in a dense district and few in a sparse one. Selecting the nearest ten addresses instead changes how far the area must extend.
The analogy explains radius versus count. In data analysis, however, “distance” can be computed from many features, and a circular map boundary may not resemble the resulting region. A convenient neighborhood is not necessarily meaningful for the prediction being made.
Worked example
On the real number line, use distance d(x,z)=|x−z| and query x=5. The candidate observations are 2, 4, 5, 6, and 8. Their distances from the query are 3, 1, 0, 1, and 3.
With closed radius r=1, the dataset neighborhood is {4,5,6}. With the strict condition d<1, it is {5}. If the query's own observation must be excluded, these become {4,6} and the empty set. A boundary convention and a self-inclusion rule have changed the usable observations without changing the data.
For the two nearest other observations, the result is {4,6}. For only one nearest other observation, 4 and 6 tie; a reproducible implementation needs a tie policy.
Now consider a separate unweighted graph A - B - C - D. A's immediate open neighborhood is {B}; its closed neighborhood is {A,B}. The nodes within at most two hops, including A, are {A,B,C}. This example distinguishes adjacency, inclusion of the center, and a larger path-based region. No numerical feature coordinates are needed to define those graph neighborhoods.
Illustrative case study
Fictional case study: a maintenance team estimates repair duration from similar historical jobs. A fixed-radius method works on common job types but returns no examples for an unusual job. Another implementation silently expands the radius until it finds five records, producing an answer based on much less similar cases.
The team makes this fallback explicit and displays the number and distance of supporting records. It also defines how feature scaling is learned from training data. The lesson is that a neighborhood rule affects both the prediction and the evidence available to explain it; no accuracy improvement is assumed.
Common misconceptions and mistakes
Do not use “local” without specifying the relevant space. A local feature-space neighborhood can cross physical, organizational, or demographic boundaries.
Do not assume the query is always excluded or included. APIs can differ depending on whether a query set is supplied explicitly.
Do not overlook scale. If one numerical feature has much larger units than another, it can dominate an unadjusted distance. Also distinguish an empty neighborhood from an error in data retrieval: it may be a legitimate consequence of the chosen radius.
Business use
Neighborhoods support recommendations, comparisons, anomaly detection, and local explanations. Their business meaning comes from the feature representation and selection rule rather than from the word alone.
Document the metric, preprocessing, radius or count, tie policy, and fallback for insufficient neighbors. Inspect representative and unusual queries to see whether the selected cases are useful comparisons. If the dataset changes density over time, a fixed radius and a fixed count can behave differently even when their settings remain unchanged.
People also ask
Is a neighborhood always a circle or sphere?
No. Its shape depends on the metric and space; graph neighborhoods are defined through edges or paths.
Is a k-neighbor set the same as a radius neighborhood?
Not generally. One fixes the number selected, while the other fixes a distance threshold.
Does a neighborhood contain its center?
That depends on the convention and application. State it explicitly, particularly when querying the training observations themselves.
Check your understanding
In the number-line example, why does changing ≤1 to <1 remove observations 4 and 6?
Answer: Both are exactly distance one from the query 5. A closed threshold includes boundary points, while a strict threshold excludes them.
Related terms
- Metric space
- Distance
- Nearest neighbor
- Local model
- radius
- adjacency
- open ball
- density-based clustering
- feature scaling
Learn next
compare fixed-radius and fixed-count neighborhoods in a dataset with dense and sparse regions. Record the number of selected observations and their farthest distance, then test how changing feature units changes the selected cases.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Sources: Aalto Dictionary of Machine Learning, neighborhood: https://aaltodictionaryofml.github.io/ADictML_Main.pdf ; scikit-learn, Nearest Neighbors: https://scikit-learn.org/stable/modules/neighbors.html ; NetworkX, ego_graph: https://networkx.org/documentation/stable/reference/generated/networkx.generators.ego.ego_graph.html . Relevant primary neighborhood and neighbor-selection guidance were read. The supplied closed-radius convention is distinguished from open-ball and graph conventions. Numerical and graph examples are original.
Previous: Neighbor Next: Neighborhood Components Analysis