PyNNDescent

0.6.0 · active · verified Thu Apr 09

PyNNDescent is a Python library that provides a fast and flexible implementation of Nearest Neighbor Descent for approximate nearest neighbor search and k-neighbor-graph construction. It supports a wide variety of distance metrics, sparse matrix inputs, and integrates with Scikit-learn. The current version is 0.6.0, and it maintains a regular release cadence with several minor patches and updates throughout the year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `NNDescent` with training data, prepare the index, and then query for approximate nearest neighbors. It generates random data for demonstration purposes.

import numpy as np
from pynndescent import NNDescent

# Generate some sample data
data = np.random.rand(1000, 64).astype(np.float32)

# Build the NNDescent index
# n_neighbors specifies the number of neighbors to find for each point
# verbose=True shows progress
index = NNDescent(data, n_neighbors=15, verbose=True)

# Build the index (computes the nearest neighbor graph)
index.prepare()

# Query the index for the 5 nearest neighbors of new data
query_data = np.random.rand(10, 64).astype(np.float32)
neighbors, distances = index.query(query_data, k=5)

print("Shape of neighbors (query_points, k):"), print(neighbors.shape)
print("Shape of distances (query_points, k):"), print(distances.shape)
print("First query point's 5 nearest neighbor indices:"), print(neighbors[0])
print("First query point's 5 nearest neighbor distances:"), print(distances[0])

view raw JSON →