annoy-binary 1.17.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- annoy_binary-1.17.3/LICENSE +202 -0
- annoy_binary-1.17.3/MANIFEST.in +4 -0
- annoy_binary-1.17.3/PKG-INFO +180 -0
- annoy_binary-1.17.3/README.rst +156 -0
- annoy_binary-1.17.3/ann.png +0 -0
- annoy_binary-1.17.3/annoy/__init__.py +16 -0
- annoy_binary-1.17.3/annoy/__init__.pyi +45 -0
- annoy_binary-1.17.3/annoy/py.typed +0 -0
- annoy_binary-1.17.3/annoy_binary.egg-info/PKG-INFO +180 -0
- annoy_binary-1.17.3/annoy_binary.egg-info/SOURCES.txt +17 -0
- annoy_binary-1.17.3/annoy_binary.egg-info/dependency_links.txt +1 -0
- annoy_binary-1.17.3/annoy_binary.egg-info/top_level.txt +1 -0
- annoy_binary-1.17.3/setup.cfg +8 -0
- annoy_binary-1.17.3/setup.py +101 -0
- annoy_binary-1.17.3/src/annoylib.h +1515 -0
- annoy_binary-1.17.3/src/annoymodule.cc +685 -0
- annoy_binary-1.17.3/src/kissrandom.h +120 -0
- annoy_binary-1.17.3/src/mman.h +242 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2021 (c) Spotify and its affiliates.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: annoy-binary
|
|
3
|
+
Version: 1.17.3
|
|
4
|
+
Summary: Approximate Nearest Neighbors in C++/Python optimized for memory usage and loading/saving to disk. Prebuilt binary
|
|
5
|
+
Home-page: https://github.com/spotify/annoy
|
|
6
|
+
Author: Erik Bernhardsson
|
|
7
|
+
Author-email: mail@erikbern.com
|
|
8
|
+
License: Apache License 2.0
|
|
9
|
+
Keywords: nns,approximate nearest neighbor search
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
.. note::
|
|
16
|
+
|
|
17
|
+
For the latest source, discussion, etc, please visit the
|
|
18
|
+
`GitHub repository <https://github.com/spotify/annoy>`_
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
.. image:: https://img.shields.io/github/stars/spotify/annoy.svg
|
|
23
|
+
:target: https://github.com/spotify/annoy
|
|
24
|
+
|
|
25
|
+
Annoy
|
|
26
|
+
-----
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
.. figure:: https://raw.github.com/spotify/annoy/master/ann.png
|
|
31
|
+
:alt: Annoy example
|
|
32
|
+
:align: center
|
|
33
|
+
|
|
34
|
+
.. image:: https://github.com/spotify/annoy/actions/workflows/ci.yml/badge.svg
|
|
35
|
+
:target: https://github.com/spotify/annoy/actions
|
|
36
|
+
|
|
37
|
+
Annoy (`Approximate Nearest Neighbors <http://en.wikipedia.org/wiki/Nearest_neighbor_search#Approximate_nearest_neighbor>`__ Oh Yeah) is a C++ library with Python bindings to search for points in space that are close to a given query point. It also creates large read-only file-based data structures that are `mmapped <https://en.wikipedia.org/wiki/Mmap>`__ into memory so that many processes may share the same data.
|
|
38
|
+
|
|
39
|
+
Install
|
|
40
|
+
-------
|
|
41
|
+
|
|
42
|
+
To install, simply do ``pip install --user annoy`` to pull down the latest version from `PyPI <https://pypi.python.org/pypi/annoy>`_.
|
|
43
|
+
|
|
44
|
+
For the C++ version, just clone the repo and ``#include "annoylib.h"``.
|
|
45
|
+
|
|
46
|
+
Background
|
|
47
|
+
----------
|
|
48
|
+
|
|
49
|
+
There are some other libraries to do nearest neighbor search. Annoy is almost as fast as the fastest libraries, (see below), but there is actually another feature that really sets Annoy apart: it has the ability to **use static files as indexes**. In particular, this means you can **share index across processes**. Annoy also decouples creating indexes from loading them, so you can pass around indexes as files and map them into memory quickly. Another nice thing of Annoy is that it tries to minimize memory footprint so the indexes are quite small.
|
|
50
|
+
|
|
51
|
+
Why is this useful? If you want to find nearest neighbors and you have many CPU's, you only need to build the index once. You can also pass around and distribute static files to use in production environment, in Hadoop jobs, etc. Any process will be able to load (mmap) the index into memory and will be able to do lookups immediately.
|
|
52
|
+
|
|
53
|
+
We use it at `Spotify <http://www.spotify.com/>`__ for music recommendations. After running matrix factorization algorithms, every user/item can be represented as a vector in f-dimensional space. This library helps us search for similar users/items. We have many millions of tracks in a high-dimensional space, so memory usage is a prime concern.
|
|
54
|
+
|
|
55
|
+
Annoy was built by `Erik Bernhardsson <http://www.erikbern.com>`__ in a couple of afternoons during `Hack Week <http://labs.spotify.com/2013/02/15/organizing-a-hack-week/>`__.
|
|
56
|
+
|
|
57
|
+
Summary of features
|
|
58
|
+
-------------------
|
|
59
|
+
|
|
60
|
+
* `Euclidean distance <https://en.wikipedia.org/wiki/Euclidean_distance>`__, `Manhattan distance <https://en.wikipedia.org/wiki/Taxicab_geometry>`__, `cosine distance <https://en.wikipedia.org/wiki/Cosine_similarity>`__, `Hamming distance <https://en.wikipedia.org/wiki/Hamming_distance>`__, or `Dot (Inner) Product distance <https://en.wikipedia.org/wiki/Dot_product>`__
|
|
61
|
+
* Cosine distance is equivalent to Euclidean distance of normalized vectors = sqrt(2-2*cos(u, v))
|
|
62
|
+
* Works better if you don't have too many dimensions (like <100) but seems to perform surprisingly well even up to 1,000 dimensions
|
|
63
|
+
* Small memory usage
|
|
64
|
+
* Lets you share memory between multiple processes
|
|
65
|
+
* Index creation is separate from lookup (in particular you can not add more items once the tree has been created)
|
|
66
|
+
* Native Python support, tested with 2.7, 3.6, and 3.7.
|
|
67
|
+
* Build index on disk to enable indexing big datasets that won't fit into memory (contributed by `Rene Hollander <https://github.com/ReneHollander>`__)
|
|
68
|
+
|
|
69
|
+
Python code example
|
|
70
|
+
-------------------
|
|
71
|
+
|
|
72
|
+
.. code-block:: python
|
|
73
|
+
|
|
74
|
+
from annoy import AnnoyIndex
|
|
75
|
+
import random
|
|
76
|
+
|
|
77
|
+
f = 40 # Length of item vector that will be indexed
|
|
78
|
+
|
|
79
|
+
t = AnnoyIndex(f, 'angular')
|
|
80
|
+
for i in range(1000):
|
|
81
|
+
v = [random.gauss(0, 1) for z in range(f)]
|
|
82
|
+
t.add_item(i, v)
|
|
83
|
+
|
|
84
|
+
t.build(10) # 10 trees
|
|
85
|
+
t.save('test.ann')
|
|
86
|
+
|
|
87
|
+
# ...
|
|
88
|
+
|
|
89
|
+
u = AnnoyIndex(f, 'angular')
|
|
90
|
+
u.load('test.ann') # super fast, will just mmap the file
|
|
91
|
+
print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors
|
|
92
|
+
|
|
93
|
+
Right now it only accepts integers as identifiers for items. Note that it will allocate memory for max(id)+1 items because it assumes your items are numbered 0 … n-1. If you need other id's, you will have to keep track of a map yourself.
|
|
94
|
+
|
|
95
|
+
Full Python API
|
|
96
|
+
---------------
|
|
97
|
+
|
|
98
|
+
* ``AnnoyIndex(f, metric)`` returns a new index that's read-write and stores vector of ``f`` dimensions. Metric can be ``"angular"``, ``"euclidean"``, ``"manhattan"``, ``"hamming"``, or ``"dot"``.
|
|
99
|
+
* ``a.add_item(i, v)`` adds item ``i`` (any nonnegative integer) with vector ``v``. Note that it will allocate memory for ``max(i)+1`` items.
|
|
100
|
+
* ``a.build(n_trees, n_jobs=-1)`` builds a forest of ``n_trees`` trees. More trees gives higher precision when querying. After calling ``build``, no more items can be added. ``n_jobs`` specifies the number of threads used to build the trees. ``n_jobs=-1`` uses all available CPU cores.
|
|
101
|
+
* ``a.save(fn, prefault=False)`` saves the index to disk and loads it (see next function). After saving, no more items can be added.
|
|
102
|
+
* ``a.load(fn, prefault=False)`` loads (mmaps) an index from disk. If `prefault` is set to `True`, it will pre-read the entire file into memory (using mmap with `MAP_POPULATE`). Default is `False`.
|
|
103
|
+
* ``a.unload()`` unloads.
|
|
104
|
+
* ``a.get_nns_by_item(i, n, search_k=-1, include_distances=False)`` returns the ``n`` closest items. During the query it will inspect up to ``search_k`` nodes which defaults to ``n_trees * n`` if not provided. ``search_k`` gives you a run-time tradeoff between better accuracy and speed. If you set ``include_distances`` to ``True``, it will return a 2 element tuple with two lists in it: the second one containing all corresponding distances.
|
|
105
|
+
* ``a.get_nns_by_vector(v, n, search_k=-1, include_distances=False)`` same but query by vector ``v``.
|
|
106
|
+
* ``a.get_item_vector(i)`` returns the vector for item ``i`` that was previously added.
|
|
107
|
+
* ``a.get_distance(i, j)`` returns the distance between items ``i`` and ``j``. NOTE: this used to return the *squared* distance, but has been changed as of Aug 2016.
|
|
108
|
+
* ``a.get_n_items()`` returns the number of items in the index.
|
|
109
|
+
* ``a.get_n_trees()`` returns the number of trees in the index.
|
|
110
|
+
* ``a.on_disk_build(fn)`` prepares annoy to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)
|
|
111
|
+
* ``a.set_seed(seed)`` will initialize the random number generator with the given seed. Only used for building up the tree, i. e. only necessary to pass this before adding the items. Will have no effect after calling `a.build(n_trees)` or `a.load(fn)`.
|
|
112
|
+
|
|
113
|
+
Notes:
|
|
114
|
+
|
|
115
|
+
* There's no bounds checking performed on the values so be careful.
|
|
116
|
+
* Annoy uses Euclidean distance of normalized vectors for its angular distance, which for two vectors u,v is equal to ``sqrt(2(1-cos(u,v)))``
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
The C++ API is very similar: just ``#include "annoylib.h"`` to get access to it.
|
|
120
|
+
|
|
121
|
+
Tradeoffs
|
|
122
|
+
---------
|
|
123
|
+
|
|
124
|
+
There are just two main parameters needed to tune Annoy: the number of trees ``n_trees`` and the number of nodes to inspect during searching ``search_k``.
|
|
125
|
+
|
|
126
|
+
* ``n_trees`` is provided during build time and affects the build time and the index size. A larger value will give more accurate results, but larger indexes.
|
|
127
|
+
* ``search_k`` is provided in runtime and affects the search performance. A larger value will give more accurate results, but will take longer time to return.
|
|
128
|
+
|
|
129
|
+
If ``search_k`` is not provided, it will default to ``n * n_trees`` where ``n`` is the number of approximate nearest neighbors. Otherwise, ``search_k`` and ``n_trees`` are roughly independent, i.e. the value of ``n_trees`` will not affect search time if ``search_k`` is held constant and vice versa. Basically it's recommended to set ``n_trees`` as large as possible given the amount of memory you can afford, and it's recommended to set ``search_k`` as large as possible given the time constraints you have for the queries.
|
|
130
|
+
|
|
131
|
+
You can also accept slower search times in favour of reduced loading times, memory usage, and disk IO. On supported platforms the index is prefaulted during ``load`` and ``save``, causing the file to be pre-emptively read from disk into memory. If you set ``prefault`` to ``False``, pages of the mmapped index are instead read from disk and cached in memory on-demand, as necessary for a search to complete. This can significantly increase early search times but may be better suited for systems with low memory compared to index size, when few queries are executed against a loaded index, and/or when large areas of the index are unlikely to be relevant to search queries.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
How does it work
|
|
135
|
+
----------------
|
|
136
|
+
|
|
137
|
+
Using `random projections <http://en.wikipedia.org/wiki/Locality-sensitive_hashing#Random_projection>`__ and by building up a tree. At every intermediate node in the tree, a random hyperplane is chosen, which divides the space into two subspaces. This hyperplane is chosen by sampling two points from the subset and taking the hyperplane equidistant from them.
|
|
138
|
+
|
|
139
|
+
We do this k times so that we get a forest of trees. k has to be tuned to your need, by looking at what tradeoff you have between precision and performance.
|
|
140
|
+
|
|
141
|
+
Hamming distance (contributed by `Martin Aumüller <https://github.com/maumueller>`__) packs the data into 64-bit integers under the hood and uses built-in bit count primitives so it could be quite fast. All splits are axis-aligned.
|
|
142
|
+
|
|
143
|
+
Dot Product distance (contributed by `Peter Sobot <https://github.com/psobot>`__) reduces the provided vectors from dot (or "inner-product") space to a more query-friendly cosine space using `a method by Bachrach et al., at Microsoft Research, published in 2014 <https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/XboxInnerProduct.pdf>`__.
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
More info
|
|
148
|
+
---------
|
|
149
|
+
|
|
150
|
+
* `Dirk Eddelbuettel <https://github.com/eddelbuettel>`__ provides an `R version of Annoy <http://dirk.eddelbuettel.com/code/rcpp.annoy.html>`__.
|
|
151
|
+
* `Andy Sloane <https://github.com/a1k0n>`__ provides a `Java version of Annoy <https://github.com/spotify/annoy-java>`__ although currently limited to cosine and read-only.
|
|
152
|
+
* `Pishen Tsai <https://github.com/pishen>`__ provides a `Scala wrapper of Annoy <https://github.com/pishen/annoy4s>`__ which uses JNA to call the C++ library of Annoy.
|
|
153
|
+
* `Atsushi Tatsuma <https://github.com/yoshoku>`__ provides `Ruby bindings for Annoy <https://github.com/yoshoku/annoy.rb>`__.
|
|
154
|
+
* There is `experimental support for Go <https://github.com/spotify/annoy/blob/master/README_GO.rst>`__ provided by `Taneli Leppä <https://github.com/rosmo>`__.
|
|
155
|
+
* `Boris Nagaev <https://github.com/starius>`__ wrote `Lua bindings <https://github.com/spotify/annoy/blob/master/README_Lua.md>`__.
|
|
156
|
+
* During part of Spotify Hack Week 2016 (and a bit afterward), `Jim Kang <https://github.com/jimkang>`__ wrote `Node bindings <https://github.com/jimkang/annoy-node>`__ for Annoy.
|
|
157
|
+
* `Min-Seok Kim <https://github.com/mskimm>`__ built a `Scala version <https://github.com/mskimm/ann4s>`__ of Annoy.
|
|
158
|
+
* `hanabi1224 <https://github.com/hanabi1224>`__ built a read-only `Rust version <https://github.com/hanabi1224/RuAnnoy>`__ of Annoy, together with **dotnet, jvm and dart** read-only bindings.
|
|
159
|
+
* `Presentation from New York Machine Learning meetup <http://www.slideshare.net/erikbern/approximate-nearest-neighbor-methods-and-vector-models-nyc-ml-meetup>`__ about Annoy
|
|
160
|
+
* Annoy is available as a `conda package <https://anaconda.org/conda-forge/python-annoy>`__ on Linux, OS X, and Windows.
|
|
161
|
+
* `ann-benchmarks <https://github.com/erikbern/ann-benchmarks>`__ is a benchmark for several approximate nearest neighbor libraries. Annoy seems to be fairly competitive, especially at higher precisions:
|
|
162
|
+
|
|
163
|
+
.. figure:: https://github.com/erikbern/ann-benchmarks/raw/master/results/glove-100-angular.png
|
|
164
|
+
:alt: ANN benchmarks
|
|
165
|
+
:align: center
|
|
166
|
+
:target: https://github.com/erikbern/ann-benchmarks
|
|
167
|
+
|
|
168
|
+
Source code
|
|
169
|
+
-----------
|
|
170
|
+
|
|
171
|
+
It's all written in C++ with a handful of ugly optimizations for performance and memory usage. You have been warned :)
|
|
172
|
+
|
|
173
|
+
The code should support Windows, thanks to `Qiang Kou <https://github.com/thirdwing>`__ and `Timothy Riley <https://github.com/tjrileywisc>`__.
|
|
174
|
+
|
|
175
|
+
To run the tests, execute `python setup.py nosetests`. The test suite includes a big real world dataset that is downloaded from the internet, so it will take a few minutes to execute.
|
|
176
|
+
|
|
177
|
+
Discuss
|
|
178
|
+
-------
|
|
179
|
+
|
|
180
|
+
Feel free to post any questions or comments to the `annoy-user <https://groups.google.com/group/annoy-user>`__ group. I'm `@fulhack <https://twitter.com/fulhack>`__ on Twitter.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Annoy
|
|
2
|
+
-----
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
.. figure:: https://raw.github.com/spotify/annoy/master/ann.png
|
|
7
|
+
:alt: Annoy example
|
|
8
|
+
:align: center
|
|
9
|
+
|
|
10
|
+
.. image:: https://github.com/spotify/annoy/actions/workflows/ci.yml/badge.svg
|
|
11
|
+
:target: https://github.com/spotify/annoy/actions
|
|
12
|
+
|
|
13
|
+
Annoy (`Approximate Nearest Neighbors <http://en.wikipedia.org/wiki/Nearest_neighbor_search#Approximate_nearest_neighbor>`__ Oh Yeah) is a C++ library with Python bindings to search for points in space that are close to a given query point. It also creates large read-only file-based data structures that are `mmapped <https://en.wikipedia.org/wiki/Mmap>`__ into memory so that many processes may share the same data.
|
|
14
|
+
|
|
15
|
+
Install
|
|
16
|
+
-------
|
|
17
|
+
|
|
18
|
+
To install, simply do ``pip install --user annoy`` to pull down the latest version from `PyPI <https://pypi.python.org/pypi/annoy>`_.
|
|
19
|
+
|
|
20
|
+
For the C++ version, just clone the repo and ``#include "annoylib.h"``.
|
|
21
|
+
|
|
22
|
+
Background
|
|
23
|
+
----------
|
|
24
|
+
|
|
25
|
+
There are some other libraries to do nearest neighbor search. Annoy is almost as fast as the fastest libraries, (see below), but there is actually another feature that really sets Annoy apart: it has the ability to **use static files as indexes**. In particular, this means you can **share index across processes**. Annoy also decouples creating indexes from loading them, so you can pass around indexes as files and map them into memory quickly. Another nice thing of Annoy is that it tries to minimize memory footprint so the indexes are quite small.
|
|
26
|
+
|
|
27
|
+
Why is this useful? If you want to find nearest neighbors and you have many CPU's, you only need to build the index once. You can also pass around and distribute static files to use in production environment, in Hadoop jobs, etc. Any process will be able to load (mmap) the index into memory and will be able to do lookups immediately.
|
|
28
|
+
|
|
29
|
+
We use it at `Spotify <http://www.spotify.com/>`__ for music recommendations. After running matrix factorization algorithms, every user/item can be represented as a vector in f-dimensional space. This library helps us search for similar users/items. We have many millions of tracks in a high-dimensional space, so memory usage is a prime concern.
|
|
30
|
+
|
|
31
|
+
Annoy was built by `Erik Bernhardsson <http://www.erikbern.com>`__ in a couple of afternoons during `Hack Week <http://labs.spotify.com/2013/02/15/organizing-a-hack-week/>`__.
|
|
32
|
+
|
|
33
|
+
Summary of features
|
|
34
|
+
-------------------
|
|
35
|
+
|
|
36
|
+
* `Euclidean distance <https://en.wikipedia.org/wiki/Euclidean_distance>`__, `Manhattan distance <https://en.wikipedia.org/wiki/Taxicab_geometry>`__, `cosine distance <https://en.wikipedia.org/wiki/Cosine_similarity>`__, `Hamming distance <https://en.wikipedia.org/wiki/Hamming_distance>`__, or `Dot (Inner) Product distance <https://en.wikipedia.org/wiki/Dot_product>`__
|
|
37
|
+
* Cosine distance is equivalent to Euclidean distance of normalized vectors = sqrt(2-2*cos(u, v))
|
|
38
|
+
* Works better if you don't have too many dimensions (like <100) but seems to perform surprisingly well even up to 1,000 dimensions
|
|
39
|
+
* Small memory usage
|
|
40
|
+
* Lets you share memory between multiple processes
|
|
41
|
+
* Index creation is separate from lookup (in particular you can not add more items once the tree has been created)
|
|
42
|
+
* Native Python support, tested with 2.7, 3.6, and 3.7.
|
|
43
|
+
* Build index on disk to enable indexing big datasets that won't fit into memory (contributed by `Rene Hollander <https://github.com/ReneHollander>`__)
|
|
44
|
+
|
|
45
|
+
Python code example
|
|
46
|
+
-------------------
|
|
47
|
+
|
|
48
|
+
.. code-block:: python
|
|
49
|
+
|
|
50
|
+
from annoy import AnnoyIndex
|
|
51
|
+
import random
|
|
52
|
+
|
|
53
|
+
f = 40 # Length of item vector that will be indexed
|
|
54
|
+
|
|
55
|
+
t = AnnoyIndex(f, 'angular')
|
|
56
|
+
for i in range(1000):
|
|
57
|
+
v = [random.gauss(0, 1) for z in range(f)]
|
|
58
|
+
t.add_item(i, v)
|
|
59
|
+
|
|
60
|
+
t.build(10) # 10 trees
|
|
61
|
+
t.save('test.ann')
|
|
62
|
+
|
|
63
|
+
# ...
|
|
64
|
+
|
|
65
|
+
u = AnnoyIndex(f, 'angular')
|
|
66
|
+
u.load('test.ann') # super fast, will just mmap the file
|
|
67
|
+
print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors
|
|
68
|
+
|
|
69
|
+
Right now it only accepts integers as identifiers for items. Note that it will allocate memory for max(id)+1 items because it assumes your items are numbered 0 … n-1. If you need other id's, you will have to keep track of a map yourself.
|
|
70
|
+
|
|
71
|
+
Full Python API
|
|
72
|
+
---------------
|
|
73
|
+
|
|
74
|
+
* ``AnnoyIndex(f, metric)`` returns a new index that's read-write and stores vector of ``f`` dimensions. Metric can be ``"angular"``, ``"euclidean"``, ``"manhattan"``, ``"hamming"``, or ``"dot"``.
|
|
75
|
+
* ``a.add_item(i, v)`` adds item ``i`` (any nonnegative integer) with vector ``v``. Note that it will allocate memory for ``max(i)+1`` items.
|
|
76
|
+
* ``a.build(n_trees, n_jobs=-1)`` builds a forest of ``n_trees`` trees. More trees gives higher precision when querying. After calling ``build``, no more items can be added. ``n_jobs`` specifies the number of threads used to build the trees. ``n_jobs=-1`` uses all available CPU cores.
|
|
77
|
+
* ``a.save(fn, prefault=False)`` saves the index to disk and loads it (see next function). After saving, no more items can be added.
|
|
78
|
+
* ``a.load(fn, prefault=False)`` loads (mmaps) an index from disk. If `prefault` is set to `True`, it will pre-read the entire file into memory (using mmap with `MAP_POPULATE`). Default is `False`.
|
|
79
|
+
* ``a.unload()`` unloads.
|
|
80
|
+
* ``a.get_nns_by_item(i, n, search_k=-1, include_distances=False)`` returns the ``n`` closest items. During the query it will inspect up to ``search_k`` nodes which defaults to ``n_trees * n`` if not provided. ``search_k`` gives you a run-time tradeoff between better accuracy and speed. If you set ``include_distances`` to ``True``, it will return a 2 element tuple with two lists in it: the second one containing all corresponding distances.
|
|
81
|
+
* ``a.get_nns_by_vector(v, n, search_k=-1, include_distances=False)`` same but query by vector ``v``.
|
|
82
|
+
* ``a.get_item_vector(i)`` returns the vector for item ``i`` that was previously added.
|
|
83
|
+
* ``a.get_distance(i, j)`` returns the distance between items ``i`` and ``j``. NOTE: this used to return the *squared* distance, but has been changed as of Aug 2016.
|
|
84
|
+
* ``a.get_n_items()`` returns the number of items in the index.
|
|
85
|
+
* ``a.get_n_trees()`` returns the number of trees in the index.
|
|
86
|
+
* ``a.on_disk_build(fn)`` prepares annoy to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)
|
|
87
|
+
* ``a.set_seed(seed)`` will initialize the random number generator with the given seed. Only used for building up the tree, i. e. only necessary to pass this before adding the items. Will have no effect after calling `a.build(n_trees)` or `a.load(fn)`.
|
|
88
|
+
|
|
89
|
+
Notes:
|
|
90
|
+
|
|
91
|
+
* There's no bounds checking performed on the values so be careful.
|
|
92
|
+
* Annoy uses Euclidean distance of normalized vectors for its angular distance, which for two vectors u,v is equal to ``sqrt(2(1-cos(u,v)))``
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
The C++ API is very similar: just ``#include "annoylib.h"`` to get access to it.
|
|
96
|
+
|
|
97
|
+
Tradeoffs
|
|
98
|
+
---------
|
|
99
|
+
|
|
100
|
+
There are just two main parameters needed to tune Annoy: the number of trees ``n_trees`` and the number of nodes to inspect during searching ``search_k``.
|
|
101
|
+
|
|
102
|
+
* ``n_trees`` is provided during build time and affects the build time and the index size. A larger value will give more accurate results, but larger indexes.
|
|
103
|
+
* ``search_k`` is provided in runtime and affects the search performance. A larger value will give more accurate results, but will take longer time to return.
|
|
104
|
+
|
|
105
|
+
If ``search_k`` is not provided, it will default to ``n * n_trees`` where ``n`` is the number of approximate nearest neighbors. Otherwise, ``search_k`` and ``n_trees`` are roughly independent, i.e. the value of ``n_trees`` will not affect search time if ``search_k`` is held constant and vice versa. Basically it's recommended to set ``n_trees`` as large as possible given the amount of memory you can afford, and it's recommended to set ``search_k`` as large as possible given the time constraints you have for the queries.
|
|
106
|
+
|
|
107
|
+
You can also accept slower search times in favour of reduced loading times, memory usage, and disk IO. On supported platforms the index is prefaulted during ``load`` and ``save``, causing the file to be pre-emptively read from disk into memory. If you set ``prefault`` to ``False``, pages of the mmapped index are instead read from disk and cached in memory on-demand, as necessary for a search to complete. This can significantly increase early search times but may be better suited for systems with low memory compared to index size, when few queries are executed against a loaded index, and/or when large areas of the index are unlikely to be relevant to search queries.
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
How does it work
|
|
111
|
+
----------------
|
|
112
|
+
|
|
113
|
+
Using `random projections <http://en.wikipedia.org/wiki/Locality-sensitive_hashing#Random_projection>`__ and by building up a tree. At every intermediate node in the tree, a random hyperplane is chosen, which divides the space into two subspaces. This hyperplane is chosen by sampling two points from the subset and taking the hyperplane equidistant from them.
|
|
114
|
+
|
|
115
|
+
We do this k times so that we get a forest of trees. k has to be tuned to your need, by looking at what tradeoff you have between precision and performance.
|
|
116
|
+
|
|
117
|
+
Hamming distance (contributed by `Martin Aumüller <https://github.com/maumueller>`__) packs the data into 64-bit integers under the hood and uses built-in bit count primitives so it could be quite fast. All splits are axis-aligned.
|
|
118
|
+
|
|
119
|
+
Dot Product distance (contributed by `Peter Sobot <https://github.com/psobot>`__) reduces the provided vectors from dot (or "inner-product") space to a more query-friendly cosine space using `a method by Bachrach et al., at Microsoft Research, published in 2014 <https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/XboxInnerProduct.pdf>`__.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
More info
|
|
124
|
+
---------
|
|
125
|
+
|
|
126
|
+
* `Dirk Eddelbuettel <https://github.com/eddelbuettel>`__ provides an `R version of Annoy <http://dirk.eddelbuettel.com/code/rcpp.annoy.html>`__.
|
|
127
|
+
* `Andy Sloane <https://github.com/a1k0n>`__ provides a `Java version of Annoy <https://github.com/spotify/annoy-java>`__ although currently limited to cosine and read-only.
|
|
128
|
+
* `Pishen Tsai <https://github.com/pishen>`__ provides a `Scala wrapper of Annoy <https://github.com/pishen/annoy4s>`__ which uses JNA to call the C++ library of Annoy.
|
|
129
|
+
* `Atsushi Tatsuma <https://github.com/yoshoku>`__ provides `Ruby bindings for Annoy <https://github.com/yoshoku/annoy.rb>`__.
|
|
130
|
+
* There is `experimental support for Go <https://github.com/spotify/annoy/blob/master/README_GO.rst>`__ provided by `Taneli Leppä <https://github.com/rosmo>`__.
|
|
131
|
+
* `Boris Nagaev <https://github.com/starius>`__ wrote `Lua bindings <https://github.com/spotify/annoy/blob/master/README_Lua.md>`__.
|
|
132
|
+
* During part of Spotify Hack Week 2016 (and a bit afterward), `Jim Kang <https://github.com/jimkang>`__ wrote `Node bindings <https://github.com/jimkang/annoy-node>`__ for Annoy.
|
|
133
|
+
* `Min-Seok Kim <https://github.com/mskimm>`__ built a `Scala version <https://github.com/mskimm/ann4s>`__ of Annoy.
|
|
134
|
+
* `hanabi1224 <https://github.com/hanabi1224>`__ built a read-only `Rust version <https://github.com/hanabi1224/RuAnnoy>`__ of Annoy, together with **dotnet, jvm and dart** read-only bindings.
|
|
135
|
+
* `Presentation from New York Machine Learning meetup <http://www.slideshare.net/erikbern/approximate-nearest-neighbor-methods-and-vector-models-nyc-ml-meetup>`__ about Annoy
|
|
136
|
+
* Annoy is available as a `conda package <https://anaconda.org/conda-forge/python-annoy>`__ on Linux, OS X, and Windows.
|
|
137
|
+
* `ann-benchmarks <https://github.com/erikbern/ann-benchmarks>`__ is a benchmark for several approximate nearest neighbor libraries. Annoy seems to be fairly competitive, especially at higher precisions:
|
|
138
|
+
|
|
139
|
+
.. figure:: https://github.com/erikbern/ann-benchmarks/raw/master/results/glove-100-angular.png
|
|
140
|
+
:alt: ANN benchmarks
|
|
141
|
+
:align: center
|
|
142
|
+
:target: https://github.com/erikbern/ann-benchmarks
|
|
143
|
+
|
|
144
|
+
Source code
|
|
145
|
+
-----------
|
|
146
|
+
|
|
147
|
+
It's all written in C++ with a handful of ugly optimizations for performance and memory usage. You have been warned :)
|
|
148
|
+
|
|
149
|
+
The code should support Windows, thanks to `Qiang Kou <https://github.com/thirdwing>`__ and `Timothy Riley <https://github.com/tjrileywisc>`__.
|
|
150
|
+
|
|
151
|
+
To run the tests, execute `python setup.py nosetests`. The test suite includes a big real world dataset that is downloaded from the internet, so it will take a few minutes to execute.
|
|
152
|
+
|
|
153
|
+
Discuss
|
|
154
|
+
-------
|
|
155
|
+
|
|
156
|
+
Feel free to post any questions or comments to the `annoy-user <https://groups.google.com/group/annoy-user>`__ group. I'm `@fulhack <https://twitter.com/fulhack>`__ on Twitter.
|
|
Binary file
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Copyright (c) 2013 Spotify AB
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
4
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
|
5
|
+
# the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
11
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
12
|
+
# License for the specific language governing permissions and limitations under
|
|
13
|
+
# the License.
|
|
14
|
+
|
|
15
|
+
# This module is a dummy wrapper around the underlying C++ module.
|
|
16
|
+
from .annoylib import Annoy as AnnoyIndex
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
from typing import Sized, overload
|
|
3
|
+
from typing_extensions import Literal, Protocol
|
|
4
|
+
|
|
5
|
+
class _Vector(Protocol, Sized):
|
|
6
|
+
def __getitem__(self, __index: int) -> float: ...
|
|
7
|
+
|
|
8
|
+
class AnnoyIndex:
|
|
9
|
+
f: int
|
|
10
|
+
def __init__(self, f: int, metric: Literal["angular", "euclidean", "manhattan", "hamming", "dot"]) -> None: ...
|
|
11
|
+
def load(self, fn: str, prefault: bool = ...) -> Literal[True]: ...
|
|
12
|
+
def save(self, fn: str, prefault: bool = ...) -> Literal[True]: ...
|
|
13
|
+
@overload
|
|
14
|
+
def get_nns_by_item(self, i: int, n: int, search_k: int = ..., include_distances: Literal[False] = ...) -> list[int]: ...
|
|
15
|
+
@overload
|
|
16
|
+
def get_nns_by_item(
|
|
17
|
+
self, i: int, n: int, search_k: int, include_distances: Literal[True]
|
|
18
|
+
) -> tuple[list[int], list[float]]: ...
|
|
19
|
+
@overload
|
|
20
|
+
def get_nns_by_item(
|
|
21
|
+
self, i: int, n: int, search_k: int = ..., *, include_distances: Literal[True]
|
|
22
|
+
) -> tuple[list[int], list[float]]: ...
|
|
23
|
+
@overload
|
|
24
|
+
def get_nns_by_vector(
|
|
25
|
+
self, vector: _Vector, n: int, search_k: int = ..., include_distances: Literal[False] = ...
|
|
26
|
+
) -> list[int]: ...
|
|
27
|
+
@overload
|
|
28
|
+
def get_nns_by_vector(
|
|
29
|
+
self, vector: _Vector, n: int, search_k: int, include_distances: Literal[True]
|
|
30
|
+
) -> tuple[list[int], list[float]]: ...
|
|
31
|
+
@overload
|
|
32
|
+
def get_nns_by_vector(
|
|
33
|
+
self, vector: _Vector, n: int, search_k: int = ..., *, include_distances: Literal[True]
|
|
34
|
+
) -> tuple[list[int], list[float]]: ...
|
|
35
|
+
def get_item_vector(self, __i: int) -> list[float]: ...
|
|
36
|
+
def add_item(self, i: int, vector: _Vector) -> None: ...
|
|
37
|
+
def on_disk_build(self, fn: str) -> Literal[True]: ...
|
|
38
|
+
def build(self, n_trees: int, n_jobs: int = ...) -> Literal[True]: ...
|
|
39
|
+
def unbuild(self) -> Literal[True]: ...
|
|
40
|
+
def unload(self) -> Literal[True]: ...
|
|
41
|
+
def get_distance(self, __i: int, __j: int) -> float: ...
|
|
42
|
+
def get_n_items(self) -> int: ...
|
|
43
|
+
def get_n_trees(self) -> int: ...
|
|
44
|
+
def verbose(self, __v: bool) -> Literal[True]: ...
|
|
45
|
+
def set_seed(self, __s: int) -> None: ...
|
|
File without changes
|