Awesome Open Source
Awesome Open Source


|pypi| |docs| |travis| |license|

.. begin_badges

.. |docs| image:: :target: :alt: Documentation Status

.. |pypi| image:: :target: :alt: Latest Version

.. |travis| image:: :target: :alt: Build Status

.. |license| image:: :target: :alt: Apache-License 2.0

.. end_badges

.. begin_intro

Lightweight approximate N\ earest N\ eighbor algorithm library written in C++ (with Python/Go bindings).

N2 stands for two N's, which comes from 'Approximate N\ earest N\ eighbor Algorithm'.

.. end_intro

.. begin_background

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.

.. end_background

.. begin_features


  • 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.

.. end_features

Supported Distance Metrics

.. Please manually sync the table below with that of docs/index.rst.

+-----------+-------------+--------------------------------------------------------------------+ | Metric | Definition | d(p, q) | +-----------+-------------+--------------------------------------------------------------------+ | "angular" | 1 - cosθ | 1 - {sum(p :sub:i · q :sub:i) / | | | | sqrt(sum(p :sub:i · p :sub:i) · sum(q :sub:i · q :sub:i))} | +-----------+-------------+--------------------------------------------------------------------+ | "L2" | squared L2 | sum{(p :sub:i - q :sub:i) :sup:2} | +-----------+-------------+--------------------------------------------------------------------+ | "dot" | dot product | sum(p :sub:i · q :sub:i) | +-----------+-------------+--------------------------------------------------------------------+

.. begin_metric_detail

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.

.. end_metric_detail


  1. Install N2 with pip.

.. code:: bash

$ pip install n2

  1. Here is a python code snippet demonstrating how to use N2.

.. code:: python

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 n2.readthedocs.io_ for full documentation. The documentation site explains the following contents in detail.

  • Installation Guide_

    • how to build from source, etc.
  • User Guide with Basic Examples

    • Python Interface_
    • C++ Interface_
    • Go Interface_
  • Benchmark_

    • detailed explanation of how we performed benchmark experiemnts.


  • 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


.. begin_footnote


- Y\. A. Malkov and D. A. Yashunin, "Efficient and robust approximate 
  nearest neighbor search using hierarchical navigable small world 
  graphs," CoRR, vol. abs/1603.09320, 2016. [Online]. 
-  Annoy:


This software is licensed under the `Apache 2 license`_, quoted below.

Copyright 2017 Kakao Corp.

Licensed under the Apache License, Version 2.0 (the “License”); you may
not use this project except in compliance with the License. You may
obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
See the License for the specific language governing permissions and
limitations under the License.

.. _Apache 2 license:
.. _Annoy:
.. _Installation Guide:
.. _Python Interface:
.. _C++ Interface:
.. _Go Interface:
.. _Benchmark:

.. |image0| image:: docs/imgs/build_time/build_time_threads.png
.. |image1| image:: docs/imgs/search_time/search_time.png
.. |image2| image:: docs/imgs/mem/memory_usage.png

.. end_footnote

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (51,962
go (14,686
jupyter-notebook (6,027
machine-learning (3,528
ml (179
knn (30
nearest-neighbor-search (27
approximate-nearest-neighbor-search (18

Find Open Source By Browsing 7,000 Topics Across 59 Categories