How to Use N2 to Find Nearest Neighbors in Large Datasets


Lightweight approximate Nearest Neighbor algorithm library written in C++ (with Python/Go bindings).

N2 stands for two N's, which comes from 'Approximate Nearest Neighbor Algorithm'.

Why N2 Was Made

Before N2, there has been other great approximate nearest neighbor libraries such as Annoy and NMSLIB. However, each of them had different strengths and weaknesses regarding usability, performance, and etc. So, N2 has been developed aiming to bring the strengths of existing aKNN libraries and supplement their weaknesses.


  • Lightweight library which runs fast with large datasets.
  • Good performance in terms of index build time, search speed, and memory usage.
  • Supports multi-core CPUs for index building.
  • Supports a mmap feature by default to efficiently process large index files.
  • Supports Python/Go bindings.

Supported Distance Metrics

MetricDefinitiond(p, q)
"angular"1 - cosθ1 - {sum(p i · q i) / sqrt(sum(p i · p i) · sum(q i · q i))}
"L2"squared L2sum{(p i - q i) 2}
"dot"dot productsum(p i · q i)

N2 supports three distance metrics. For "angular" and "L2", d (distance) is defined such that the closer the vectors are, the smaller d is. However for "dot", d is defined such that the closer the vectors are, the larger d is. You may be wondering why we defined and implemented "dot" metric as plain dot product and not as (1 - dot product). The rationale for this decision was to allow users to directly interpret the d value returned from Hnsw search function as a dot product value.


  • Install N2 with pip.
$ pip install n2
  • Here is a python code snippet demonstrating how to use N2.
import numpy as np

from n2 import HnswIndex

N, dim = 10240, 20
samples = np.arange(N * dim).reshape(N, dim)

index = HnswIndex(dim)
for sample in samples:
    index.add_data(sample), n_threads=4)
print(index.search_by_id(0, 10))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Full Documentation

Visit for full documentation. The documentation site explains the following contents in detail.


  • Here are the results of our benchmark experiments.
  • You can also see benchmarks of various ANN libraries in Python at Note that N2 version 0.1.6 is used in (last checked on October 8th, 2020) and we are continuing our efforts to improve N2 performance.

Index Build Time


Search Speed


Memory Usage



Download Details:

Author: kakao
Source Code: 
License: Apache-2.0 license

#machinelearning #Python #go #ml 

How to Use N2 to Find Nearest Neighbors in Large Datasets
1.05 GEEK