liveBook · Manning

HNSW

As explained in Outlier Detection in Python

HNSW is a graph-based approximate nearest-neighbor data structure that organizes navigable connections between data points.

HNSW is a data structure for very fast, approximate nearest-neighbor searches, especially useful when numeric pairwise distance calculations must scale to large datasets. section 10.2.5

A typical workflow creates an index with a distance function, inserts records individually, and queries the index for a chosen number of neighbors. Approximation improves speed, but results should be interpreted within the needs of the downstream detector. section 10.2.5

Listing 10.7 Example using HNSW for nearest neighbors queries
from datasketch.hnsw import HNSW
import numpy as np
 
data = np.random.random_sample((1000, 10))                      #1
index = HNSW(distance_func=lambda x, y: np.linalg.norm(x - y))
for i, d in enumerate(data):                                    #2
    index.insert(i, d)
 
index.query(data[0], k=10)                                      #3

Shows a concise Python implementation using random 1,000-row, 10-feature numeric data, NumPy distance calculation, insertion, and a ten-neighbor query. section 10.2.5

How Outlier Detection in Python covers it

How deep it goes
Demonstrates index construction and querying, but stops short of explaining internal navigation mechanics or accuracy trade-offs.
Examples
Uses 1,000 random ten-feature rows, a NumPy Euclidean distance function, sequential insertion, and a ten-neighbor query.
What it assumes
Assumes numeric data and a distance function, while illustrating both through NumPy vector arithmetic.
What you can do after
Apply it via datasketch to build an index and query approximate nearest neighbors for numeric vectors.
How it connects
Connects nearest-neighbor search with pairwise distance calculations, outlier detection, and clustering performance.

Also explained in

Compare how 7 books explain HNSW →

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.